批改狀態(tài):合格
老師批語:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>1.用實(shí)例來創(chuàng)建用戶表單</title> <style type="text/css"> table { background-color: wheat; box-shadow: 3px 3px 3px #888; border-radius: 3%; padding: 15px; margin: 30px auto; } table td { padding: 8px; } table caption { font-size: 1.5em; margin-bottom: 10px; } textarea { resize: none; } form button { width: 100px; height: 30px; border: none; background-color: skyblue; color: white; } form button:hover { background-color: orangered; font-size: 1.1em; cursor: pointer; } </style> </head> <body> <form> <table> <caption>用戶注冊(cè)</caption> <tr> <td><label for="email">郵箱:</label></td> <td><input type="text" name="email" id="email" autofocus=""></td> </tr> <tr> <td><label for="password1">密碼:</label></td> <td><input type="password" name="password1" id="password1"></td> </tr> <tr> <td><label for="password2">確認(rèn):</label></td> <td><input type="password" name="password2" id="password2"></td> </tr> <tr> <td><label for="secret">性別:</label></td> <td><input type="radio" name="gender" id="male" value="male" for="male"><label for="male">男</label> <input type="radio" name="gender" id="female" value="female" for="female"><label for="female">女</label> <input type="radio" name="gender" id="secret" value="secret" checked="" for="secret"><label for="secret">保密</label></td> </tr> <tr> <td><label for="">級(jí)別:</label></td> <td> <select name="level" id="level"> <option value="0">小白</option> <option value="1" selected="">中級(jí)</option> <option value="2">大神</option> </select> </td> </tr> <tr> <td><label for="PHP">語言:</label></td> <td><input type="checkbox" name="lang[]" id="php" value="php" checked=""><label for="PHP">PHP</label> <input type="checkbox" name="lang[]" id="Java" value="Java"><label for="Java">Java</label> <input type="checkbox" name="lang[]" id="Python" value="Python"><label for="Python">Python</label> <input type="checkbox" name="lang[]" id="C++" value="C++"><label for="C++"JavaScript">C++</label></td> <tr> <td><label for="comment">簡(jiǎn)介:</label></td> <td><textarea name="conment" id="conment" cols="30" rows="3"></textarea></td> </tr> <tr> <td colspan="2" align="center"> <button type="submit" name="submit" id="submit" value="submit">提交</button> </td> </tr> </table> </form> <script type="text/javascript" src="js/jquery-3.3.1.js"></script> <script type="text/javascript"> //所有的表單數(shù)據(jù)的驗(yàn)證全部使用AJAX完成,但是為了代碼的簡(jiǎn)潔與可讀性,操作類型使用GET //郵箱驗(yàn)證 $('#email').blur(function(){ //注意:這里是獲取email的id,因此正確為:'#mail' //采用post $.post('admin/check.php?check=email','email='+$('#email').val(),function(data){ //url后面加?check=email; data用'email='+$拼接,屬性為:'#mail' , 回調(diào)函數(shù)為data ,即function(data) switch(data.status){ case 0: $('td').find('span').remove() //獲取到所有的TD,查詢所有的SPAN元素,杜絕掉! $('#email').after('<span>').next().text(data.msg).css('color','red').prev().focus() //當(dāng)輸入錯(cuò)誤,自動(dòng)把焦點(diǎn)移到SPAN前面的元素INPUT上面來并設(shè)置焦點(diǎn)! break; case 1: $('td').find('span').remove() //獲取到所有的TD,查詢所有的SPAN元素,杜絕掉! $('#email').after('<span>').next().text(data.msg).css('color','red').prev().focus() //當(dāng)輸入錯(cuò)誤,自動(dòng)把焦點(diǎn)移到SPAN前面的元素INPUT上面來并設(shè)置焦點(diǎn)! break; case 2: $('td').find('span').remove() //獲取到所有的TD,查詢所有的SPAN元素,杜絕掉! $('#email').after('<span>').next().text(data.msg).css('color','green') //直到輸入正確,郵箱可用,才允許跳轉(zhuǎn)! break; } },'json') //注意:此處是,'json' }) //密碼驗(yàn)證: $('#password1').blur(function(){ if($('#email').val().length ==0){ return false } $.post('admin/check.php?check=password1','password1='+$('#password1').val(),function(data){ if (data.status == 0) { //以下#PASSOWD的必須加引號(hào)?。?! $('#password1').after('<span>').next().text(data.msg).css('color','red').prev().focus(); $('td').find('span').first().remove(); } },'json') //此處的JSON必須要加引號(hào)!! }) //確認(rèn)密碼驗(yàn)證: $('#password2').blur(function(){ if($('#email').val().length ==0){ return false } if($('#password1').val().length ==0){ return false } $.post('admin/check.php?check=password2',{ password1: $('#password1').val(), password2: $('#password2').val() },function(data){ switch(data.status){ //根據(jù)data的statis狀態(tài)來進(jìn)行判斷 case 0: //當(dāng)前數(shù)據(jù)空值,注意:這里是冒號(hào),不是分號(hào),看清楚?。?! $('td').find('span').empty() //獲取到所有的TD,查詢所有的SPAN元素,杜絕掉! $('#password2').after('<span>').next().text(data.msg).css('color','red').prev().focus() //當(dāng)輸入錯(cuò)誤,自動(dòng)把焦點(diǎn)移到SPAN前面的元素INPUT上面來并設(shè)置焦點(diǎn)! break; case 1: $('td').find('span').remove() //獲取到所有的TD,查詢所有的SPAN元素,杜絕掉! $('#password2').after('<span>').next().text(data.msg).css('color','red').prev().focus() //當(dāng)輸入錯(cuò)誤,自動(dòng)把焦點(diǎn)移到SPAN前面的元素INPUT上面來并設(shè)置焦點(diǎn)!*/ break; case 2: $('td').find('span').remove() //獲取到所有的TD,查詢所有的SPAN元素,杜絕掉! $('#password2').after('<span>').next().text(data.msg).css('color','green') //直到輸入正確,郵箱可用,才允許跳轉(zhuǎn)! break; } },'json') //此處的JSON必須要加引號(hào)!! }) </script> </body> </html>
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
<?php // echo '<pre>'; // print_r($_POST); // echo $_GET['check']; //用url中check值進(jìn)行判斷,確定驗(yàn)證的字段 switch ($_GET['check']) { // echo '驗(yàn)證郵箱'; //測(cè)試數(shù)據(jù) case 'email': $email = $_POST['email']; //注意:設(shè)置郵箱的請(qǐng)求類型 if (empty($email)){ //不能用ISSET()驗(yàn)證,會(huì)出錯(cuò)。用empty() // exit()['status' =>0,'msg'=>'郵箱不能為空'] // 三種狀態(tài)判斷結(jié)構(gòu): exit(json_encode(['status' =>0,'msg' =>'郵箱不能為空'])); //exit(['status'=>0,'msg'=>'郵箱不能為空'])先輸出一個(gè)數(shù)組;將一個(gè)字符串進(jìn)行編碼。 } else if (in_array($email,['admin@php.cn','kevinw@php.cn'])) { //注意:這里是in_array,別寫錯(cuò)了?。?! exit(json_encode(['status' =>1,'msg' =>'郵箱已占用'])); } else { exit(json_encode(['status' =>2,'msg' =>'郵箱可用'])); } break; // 密碼驗(yàn)證 case 'password1': $password1 = $_POST['password1']; if (empty($password1)){ exit(json_encode(['status' =>0,'msg' =>'密碼不能為空'])); } break; // 確認(rèn)密碼驗(yàn)證 case 'password2': $password1 = $_POST['password1']; $password2 = $_POST['password2']; if (empty($password2)){ exit(json_encode(['status' =>0,'msg' =>'確認(rèn)密碼不能為空'])); } else if ($password1 != $password2) { exit(json_encode(['status' =>1,'msg' =>'二次密碼不相等'])); } else { exit(json_encode(['status' =>2,'msg' =>'驗(yàn)證通過'])); } break; } //注意這里的假設(shè)語句: // switch ($_GET['check']) { // case 'email': // $email = $_POST['email']; // if (empty($email)){ // exit(json_encode(['status' =>0,'msg' =>'中文'])); // } else if (in_array($mail,['admin@php.cn','kevinw@php.cn'])){ // exit(json_encode(['status' =>1,'msg' =>'中文'])); // } else { // exif(json_encode(['status' =>2,'msg' =>'中文'])) // } // break; // }
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)