<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <center> <table> <tr> <td>帐号</td> <td><input type="text" name="user" id="user"></td> </tr> <tr> <td>密码</td> <td><input type="text" name="pwd" id="pwd"></td> </tr> <tr> <td><input type="submit" value="注册"></td> </tr> </table> </center> </body> </html> <script src="./jquery-3.3.1.min.js"></script> <script> $(document).on('click',':submit',function(){ var user=$("#user").val(); var pwd=$("#pwd").val(); if (user=='' || user==null){ alert('帐号为空'); return false; } if (pwd=='' || pwd==null){ alert('密码为空'); return false; } $.ajax({ method:'post', url:'regist.php', jsonpCallback:'liyue', dataType:'jsonp', data:{ user:user, pwd:pwd }, success:function(){ console.log('执行中'); // alert('注册成功'); } }) }) function liyue(data){ alert(data.code); } </script>
<?php $user=$_POST['user']; $pwd=$_POST['pwd']; $Callback=$_GET['callback']; $pdo= new PDO("mysql:host=127.0.0.1;dbname=1703a",'root','root'); $sql="insert into users values(null,?,?)"; $query=$pdo->prepare($sql); $res=$query->execute([$user,$pwd]); if ($res){ $str=json_encode(['code'=>'注册成功','msg'=>'100','data'=>null]); }else{ $str=json_encode(['code'=>'注册失败','msg'=>'200','data'=>null]); } echo $Callback.'('.$str.')'; ?>