<!DOCTYPE html> <html> <head> <title>登录</title> <meta charset="utf-8"> </head> <body> <input type="text" name="username" class='username' placeholder="请输入账号"> <span class="s1"></span> <br> <input type="password" name="password" class='password' placeholder="请输入密码"> <span class="s2"></span> <br> <input type="submit" class="submit" value="登录"> </body> </html> <script src='jquery-3.3.1.min.js'></script> <script> $('.submit').click(function(){ var username = $('.username').val(); var password = $('.password').val(); if(username.length==0){ $('.s1').html("<font color='red'>账号不能为空</font>"); return false; } var reg = /^[a-zA-Z]{6}$/; if(reg.test (username) == to false ) { $ ( ' .s1 ' ) .html ( " <font Color = 'Red'> 6-digit account number must be alphabetic </ font> " ); return to false ; } $ ( ' .s1 ' ) .html ( " <font Color = 'Green'> qualified testing </ font> " ); IF (password.length == 0 ) { $ ( ' .s2 ' ) .html ( " <font color = 'red'> password is not blank </ font> " ); return false; } Var pass_reg = / ^ \ 3 W {,} $ / ; IF (pass_reg.test (password) == to false ) { $ ( ' .s2 ' ) .html ( " <font Color = 'Red'> 3 Minimum Password bits </ font> " ); return to false ; } $ ( ' .s2 ' ) .html ( " <font Color = 'Green'> qualified testing </ font> " ); $ .ajax ({ URL: ' Login .php ' , type:'POST ' , dataType: ' JSONP ' , jsonpCallback: ' Liyue ' , // process the data returned back Data: { username: username, password: password }, Success: function () { the console.log ( ' request complete ' ); } }) }) function Liyue (Data) { Alert (data.msg); } </ Script >
php code:
<?php header("Content-type:text/html;charset=utf-8"); //接收传值 $username = $_POST['username']; $password = $_POST['password']; //接收回调函数 $callback = $_GET['callback']; $pdo = new PDO('mysql:host=127.0.0.1;dbname=1703a','root','root'); $sql = 'select * from user1 where username=? and password=?'; $query = $pdo->prepare($sql); $query->execute([$ username , $ password ]); $ Data = $ Query -> FETCH (2 ); IF ( empty ( $ Data )) { $ RES = json_encode ([ 'code' => 400, 'MSG' => 'account password bad ',' Data '=> null ]); } the else { $ RES = json_encode ([' code '=> 200 is,' MSG '=>' login success', 'Data' => null ]); } echo the callback $ '('.. $ RES . ')';