博客列表 >jq ajax提交

jq ajax提交

张浩刚
张浩刚原创
2019年12月26日 17:17:12919浏览

post前端部分(ajax)

  1. <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
  2. <form>
  3. <p><label for="user">姓名:<input type="text" name="user" id="user"></label></p>
  4. <p><label for="password">密码:<input type="password" name="password" id="password"></label></p>
  5. <button onclick="ajax()" type="button">提交</button> //type='button'才能ajax
  6. </form>
  7. //post提交
  8. <script>
  9. function ajax(){
  10. //serialize() 获取所有能填写的数据的input等
  11. $.post('/demo.php',$('form').serialize(),function(reg){
  12. if(reg.code>0){
  13. console.log(reg.msg);
  14. }else{
  15. console.log(reg.msg);
  16. }
  17. },'json')
  18. }
  19. </script>

post后端部分(ajax)

  1. class users
  2. {
  3. public function show()
  4. {
  5. $user = $_POST['user'];
  6. $password = $_POST['password'];
  7. if($user !== 'admin'){
  8. $date = json_encode(['code'=>1; 'msg'=>'账户不正确']); //转换json数组,才可返回
  9. die($date);
  10. }
  11. if($password !== '123456'){
  12. $date = json_encode(['code'=>1; 'msg'=>'密码错误']);
  13. die($date);
  14. }
  15. $date = json_encode(['code'=>0; 'msg'=>'登陆成功']);
  16. die($date);
  17. }
  18. }
  19. $user = new users;
  20. $user->show();

get前端部分(ajax)

  1. <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
  2. <form>
  3. <p><label for="user">姓名:<input type="text" name="user" id="user"></label></p>
  4. <p><label for="password">密码:<input type="password" name="password" id="password"></label></p>
  5. <button onclick="ajax()" type="button">提交</button> //type='button'才能ajax
  6. </form>
  7. //post提交
  8. <script>
  9. function ajax(){
  10. //serialize() 获取所有能填写的数据的input等
  11. //$.get('/demo.php?user=user&password=password',function(reg){},'json')
  12. $.get('/demo.php',$('form').serialize(),function(reg){
  13. if(reg.code>0){
  14. console.log(reg.msg);
  15. }else{
  16. console.log(reg.msg);
  17. }
  18. },'json')
  19. }
  20. </script>

get后端部分(ajax)

  1. class users
  2. {
  3. public function show()
  4. {
  5. $user = $_GET['user'];
  6. $password = $_GET['password'];
  7. if($user !== 'admin'){
  8. $date = json_encode(['code'=>1; 'msg'=>'账户不正确']); //转换json数组,才可返回
  9. die($date);
  10. }
  11. if($password !== '123456'){
  12. $date = json_encode(['code'=>1; 'msg'=>'密码错误']);
  13. die($date);
  14. }
  15. $date = json_encode(['code'=>0; 'msg'=>'登陆成功']);
  16. die($date);
  17. }
  18. }
  19. $user = new users;
  20. $user->show();
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议