最近在學php的表單驗證,有必要簡單的總結一下
1)給出一個架構圖
2)具體的實現
表單的實現
<meta http-quiv='Content-Type' cotent='text/html'; charset=utf-8><form action="test2.php" method='post'>使用者名稱: <input type="text" name='username' /> <br />密碼:<input type="password" name='password' /> <br />驗證碼:<input type="code" name='code'size='5' /> 2345 <br />介紹:<textarea name="content" rows="10" cols="30"></textarea><br /><input type="submit" name='send' value='提交'/>;</form>
提交後頁面的處理及驗證
1 <?php 2 header("Content-Type:text/html;charset='utf-8'"); 3 if (!isset($_POST['send'])||$_POST['send']!='提交') { 4 header("Location:test1.php"); 5 exit; 6 } 7 8 9 //第二步,接收所有的資料10 $username=trim($_POST['username']);11 $password=$_POST['password'];12 $code=trim($_POST['code']);13 $content=htmlspecialchars($_POST['content']);14 //使用者名稱不能小於兩位,不能大於10位15 if(strlen($username)<2||strlen($username)>10){16 echo "<script>alert('使用者名稱不能小於2位大於10');17 history.back();</script>";18 exit;19 20 }21 //密碼不能小於6位22 if (strlen($password)<6) {23 echo "<script> alert('密碼不能小於不能6位');history.back();</script>";24 exit;25 26 }27 //粗步判斷驗證碼28 if (strlen($code)!=4 && !is_numeric($code)) {29 echo "<script>alert('驗證碼必須為數字而且是4位');history.back();</script>";30 exit;31 32 }33 34 echo "使用者名稱".$username;35 echo '<br/>';36 echo '個人介紹'.$content; 37 38 39 40 ?>
3)總結
1.對於中文字元亂碼的處理
在html中根據自己的vim對源碼的編碼是utf-8的格式,所以要改變編碼方式.
重要的是這句
<meta http-quiv='Content-Type' cotent='text/html'; charset=utf-8>.
在php,重要的是這句
header("Content-Type:text/html;charset='utf-8'");
2.對於表單驗證
用post傳送變數,就必須用$_pOST這個全域變數來接受,同理get 也是!
可以用超連結來傳送變數方法是 超連結?變數名=值來 傳送 變數的值,其可以用$_GET來接收.