PHP表單提交驗證各種方式

木子小僧發表於2015-05-14
  1 <!DOCTYPE unspecified PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2 <html>
  3 <title>select</title>
  4 <body>
  5 <?php 
  6 //姓名處理
  7 if (empty($_POST["username"])){
  8 $usernamerror ="input your name!!";
  9 }else {
 10 $username = test_input($_POST["username"]);
 11 }
 12 //密碼處理
 13 if (empty($_POST["userpass"]) && empty($_POST["userpassagain"])){
 14 $passworderror="input your userpass!!";
 15 }else {
 16 $password = test_input($_POST["userpass"]);
 17 $passwordagain = test_input($_POST["userpassagain"]);
 18 if($password!=$passwordagain){
 19 $passworderror="input your userpass again!!";
 20 }
 21 }
 22 //性別處理
 23 if (!isset($_POST["sex"])){
 24 @$sex = $_POST["sex"];
 25 }
 26 
 27 
 28 
 29 function test_input($date){
 30 $date = trim($date);
 31 $date = htmlspecialchars($date);
 32 $date = stripcslashes($date);
 33 return $date;
 34 }
 35 ?>
 36 <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
 37 <table>
 38 <tr>
 39 <td>Namd:</td>
 40 <td>
 41 <input type="text" name="username">
 42 <span> <?php echo @$usernamerror;?></span>
 43 </td>
 44 </tr>
 45 <tr>
 46 <td>PassWord:</td>
 47 <td>
 48 <input type="password" name="userpass">
 49 <span><?php echo @$passworderror;?></span>
 50 </td>
 51 </tr>
 52 <tr>
 53 <td>PassWordAgain:</td>
 54 <td>
 55 <input type="password" name="userpassagain">
 56 </td>
 57 </tr>
 58 <tr>
 59 <td>Sex:</td>
 60 <td>
 61 <input type="radio" name="sex" value="boy" checked> 62 <input type="radio" name="sex" value="gril"> 63 </td>
 64 </tr>
 65 <tr>
 66 <td>Fav:</td>
 67 <td>
 68 <input type="checkbox" name="fav[]" value="籃球"> 籃球
 69 <input type="checkbox" name="fav[]" value="足球"> 足球
 70 <input type="checkbox" name="fav[]" value="棒球"> 棒球 
 71 </td>
 72 </tr>
 73 <tr>
 74 <td>Manger:</td>
 75 <td>
 76 <select name="manger">
 77 <option value="工商管理">工商管理</option>
 78 <option value="軟體開發">軟體開發</option>
 79 <option value="電子商務">電子商務</option>
 80 </select>
 81 </td>
 82 </tr>
 83 <tr>
 84 <td></td>
 85 <td>
 86 <input type="submit" value="提交">
 87 </td>
 88 </tr> 
 89 </table>
 90 </form>
 91 
 92 <?php 
 93 @$sex = $_POST["sex"];
 94 echo "<h2>this is your input:</h2>";
 95 echo "<br>";
 96 echo "name:".@$username;
 97 echo "<br>";
 98 echo "password:".@$password;
 99 echo "<br>";
100 echo "you sex is:".@$sex;
101 echo "<br>";
102 //興趣處理
103 @$fav = $_POST["fav"];
104 if (@$_POST["fav"]!=null){
105 echo "your fav is:";
106 echo "<br>";
107 foreach ($_POST["fav"] as $fav){
108 echo $fav;
109 echo "<br>";
110 }
111 }
112 //下拉框處理
113 
114 echo "您選擇的意見主題為:".@$_POST[manger];
115 ?>
116 </body>
117 </html>

 

相關文章