批改狀態(tài):合格
老師批語:
通過PHP實(shí)現(xiàn)用戶注冊頁面的表單驗(yàn)證請求
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>用戶注冊驗(yàn)證</title> <style> table{ width: 400px; height: auto; border:1px solid #888; padding: 15px; margin: 10px auto; background-color: lightskyblue; border-radius: 10px; box-shadow: 2px 2px 2px #888; } table h2{ text-align: center; } table td{ padding-bottom: 10px; } button{ width: 120px; height: 35px; background-color: dodgerblue; color: #fff; border: none; } button:hover{ background-color: red; cursor: pointer; } textarea{ resize: none; } span{ font-size: 13px; } </style> </head> <body> <form action="admin/check.php" method="post"> <table> <caption><h2>用戶注冊</h2></caption> <tr> <td><label for="email">郵箱:</label></td> <td><input type="text" name="email" id="email" autofocus=""></td> </tr> <tr> <td><label for="password">密碼:</label></td> <td><input type="password" name="password" id="password"></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="secrecy">性別:</label></td> <td> <input type="radio" name="sex" id="man" value="man"><label for="man">男</label> <input type="radio" name="sex" id="famale" value="famale"><label for="famale">女</label> <input type="radio" name="sex" id="secrecy" value="secrecy" checked=""><label for="secrecy">保密</label> </td> </tr> <tr> <td><label for="level">級別:</label></td> <td> <select name="level" id=""> <option value="0">初級</option> <option value="1" selected="">中級</option> <option value="2">高級</option> </select> </td> </tr> <tr> <td><label for="">語言:</label></td> <td> <input type="checkbox" name="lang[]" id="PHP" value="php" checked>PHP <input type="checkbox" name="lang[]" id="JAVA" value="JAVA">JAVA <input type="checkbox" name="lang[]" id="C++" value="C++">C++ <input type="checkbox" name="lang[]" id="NET" value="NET">NET </td> </tr> <tr> <td><label for="text">備注:</label></td> <td> <textarea name="texts" id="texts" cols="30" rows="5" ></textarea> </td> </tr> <tr> <td colspan="2" align="center"><button>立即注冊</button></td> </tr> </table> </form> <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> <script type="text/javascript"> // 郵箱驗(yàn)證請求 $('#email').blur(function(){ //post請求,URL將請求發(fā)送到哪里去,data發(fā)送到服務(wù)器的數(shù)據(jù),function()請求成功后執(zhí)行的函數(shù) $.post('admin/check.php?check=email','email='+ $('#email').val(), function(data){ switch(data.status){ case 0: $('td').find('span').remove(); $('#email').after('<span>').next().text(data.msg).css('color', 'red').prev().focus(); break; case 1: $('td').find('span').remove(); $('#email').after('<span>').next().text(data.msg).css('color', 'red').prev().focus(); break; case 2: $('td').find('span').remove(); $('#email').after('<span>').next().text(data.msg).css('color', 'green'); } },'json') }) //密碼驗(yàn)證 $('#password').blur(function(){ //判斷前一個(gè)輸入框是否為空,如果為空將焦點(diǎn)自動(dòng)回到為空輸入框 if ($('#email').val().length == 0) { return false } $.post('admin/check.php?check=password','password='+$('#password').val(),function(data){ if(data.status == 0) { $('td').find('span').remove() $('#password').after('<span>').next().text(data.msg).css('color', 'red').prev().focus(); } },'json') }) //二次密碼驗(yàn)證 $('#password2').blur(function(){ //判斷#password是否為空,如為空將焦點(diǎn)自動(dòng)回到空輸入框中 if ($('#password').val().length == 0) { return false } $.post('admin/check.php?check=password2', { password: $('#password').val(), password2: $('#password2').val() }, function(data){ switch(data.status) { case 0: $('td').find('span').remove() $('#password2').after('<span>').next().text(data.msg).css('color', 'red').prev().focus(); break; case 1: $('td').find('span').remove() $('#password2').after('<span>').next().text(data.msg).css('color', 'red').prev().focus(); break; case 2: $('td').find('span').remove() $('#password2').after('<span>').next().text(data.msg).css('color', 'green'); break; } },'json') }) //備注文字驗(yàn)證 $('#texts').blur(function(){ if($('#password2').val().length == 0){ return false } $.post('admin/check.php?check=texts','texts='+$('#texts').val(), function(data) { switch(data.status) { case 0: $('td').find('span').remove() $('#texts').after('<span style="font-size:13px;">').next().text(data.msg).css('color', 'red').prev().focus(); break; case 1: $('td').find('span').remove() $('#texts').after('<span style="font-size:13px;">').next().text(data.msg).css('color', 'red').prev().focus(); break; case 2: $('td').find('span').remove() $('#texts').after('<span style="font-size:13px;">').next().text(data.msg).css('color', 'green'); break; } },'json') }) </script> </body> </html>
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
check.php文件
<?php switch ($_GET['check']) { //驗(yàn)證郵箱 case 'email': $email = $_POST['email']; //設(shè)置默認(rèn)值 if(empty($email)) { exit(json_encode(['status'=>0,'msg'=>'郵箱不能為空'])); } else if(in_array($email,['admin@php.cn','zhu@php.cn'])) { exit(json_encode(['status'=>1,'msg'=>'郵箱已占用'])); } else { echo json_encode(['status'=>2,'msg'=>'郵箱可用']); } break; //驗(yàn)證密碼 case 'password': $password = $_POST['password']; //設(shè)置默認(rèn)值 if(empty($password)) { exit(json_encode(['status'=>0,'msg'=>'密碼不能為空'])); } break; //二次確認(rèn)密碼驗(yàn)證 case 'password2': //獲取到第一次密碼和第二次密碼 $password = $_POST['password']; //設(shè)置默認(rèn)值 $password2 = $_POST['password2']; //設(shè)置默認(rèn)值 if(empty($password2)) { exit (json_encode(['status'=>0,'msg'=>'確認(rèn)密碼不能為空'])); } else if($password != $password2) { exit (json_encode(['status'=>1,'msg'=>'兩次密碼必須一致'])); } else { exit (json_encode(['status'=>2,'msg'=>'驗(yàn)證通過'])); } break; //備注驗(yàn)證數(shù)字 case 'texts': $texts = $_POST['texts']; if(empty($texts)) { exit (json_encode(['status'=>0,'msg'=>'字?jǐn)?shù)不能為空'])); } else if(strlen($texts)<20) { exit (json_encode(['status'=>1,'msg'=>'不能少于10個(gè)字'])); } else { exit (json_encode(['status'=>2,'msg'=>'驗(yàn)證通過'])); } break; } ?>
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
預(yù)覽效果圖:
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號