php實現簡單的表單驗證(大神勿進來)!

來源:互聯網
上載者:User

   最近在學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來接收. 








聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.