標籤:php表單驗證 php表單必填 php表單提交後資料的保留
一、表單驗證中用到的幾個元素記錄
1.htmlspecialchars(),用於將使用者輸入的特殊字元轉義為一般字元,比如 < 和 > 之類的 HTML 字元會被替換為 < 和 >
2.$_SERVER["PHP_SELF"] 是一種超全域變數,返回當前頁面指令碼名字
3.trim()用於刪除多餘的空格等
4.stripslashes()用於刪除使用者多輸入的反斜線
二、一個簡單的表單驗證函式
function test_input($str){$str = trim($str);//去除空格等$str = stripslashes($str);//去除使用者輸入的反斜線$str = htmlspecialchars($str);//轉意特殊字元為一般字元return $str;}
三、表單中必填項的提示代碼
1.php代碼中,驗證輸入框是否為空白,是則增加錯誤資訊,用於在HTML中顯示,非空則驗證和處理輸入
2.HTML代碼中插入輸出錯誤資訊的php程式碼片段如<span class="error">*<?php echo $emailErr?></span><br/>
$emailErr為儲存錯誤資訊變數,起始為空白。
簡單樣本:
<!DOCTYPE HTML> <html><head><style>.error {color: #FF0000;}</style></head><body> <?php$name = $email = $website = $area = $gender = "";$nameErr = $emailErr = ""; if ($_SERVER['REQUEST_METHOD'] == 'POST'){//attention $_SERVER['REQUEST_METHOD']if (empty($_POST['name'])){$nameErr = "姓名必填";}else{$name = test_input($_POST['name']);}if (empty($_POST['email'])){$emailErr = "電郵必填";}else{$email = test_input($_POST['email']);}if (empty($_POST['website'])){$website = "";}else{$website = test_input($_POST['website']);}if (empty($_POST['area'])){$area = "";}else{$area = test_input($_POST['area']);}if (empty($_POST['gender'])){$gender = "";}else{$gender = test_input($_POST['gender']);}}function test_input($str){$str = trim($str);//去除空格等$str = stripslashes($str);//去除使用者輸入的反斜線$str = htmlspecialchars($str);//轉意特殊字元為一般字元return $str;}?><p class="error">*為必填</p><!--attention $_SERVER['PHP_SELF']--><form method="POST" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>">姓名:<input type="text" name="name"><span class="error">*<?php echo $nameErr?></span><br/>電郵:<input type="text" name="email"><span class="error">*<?php echo $emailErr?></span><br/>網址:<input type="text" name="website"><br/>評論:<textarea name="area" rows="4" cols="40"></textarea><br/>性別:<input type="radio" name="gender" value="female">女性<input type="radio" name="gender" value="male">男性<br/><input type="submit" value="提交" name="submit"><br/></form><?phpecho "您的輸入是:<br/>";echo "姓名:",$name;echo "<br/>電郵:", $email;echo "<br/>網址:", $website;echo "<br/>評論:", $area;echo "<br/>性別:", $gender;?>
顯示裁圖:
四、表單資料的保留
在上述代碼中只需改動如下 <input>標籤中添加PHP片段:<input type=‘‘ name=‘‘ value="<?php echo $name?>">
對<textarea>元素,只需將php片段添加在<textarea></textarea>之間
對單選,較麻煩,在<input>中添加代碼:<?php if (isset($gender)) && $gender == ‘female‘) echo ‘checked‘;?>
<form method="POST" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>">姓名:<input type="text" name="name" value="<?php echo $name;?>"><span class="error">*<?php echo $nameErr?></span><br/>電郵:<input type="text" name="email" value="<?php echo $email;?>"><span class="error">*<?php echo $emailErr?></span><br/>網址:<input type="text" name="website" value="<?php echo $website;?>"><br/>評論:<textarea name="area" rows="4" cols="40"><?php echo $area;?></textarea><br/>性別:<input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female">女性<input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">男性<br/><input type="submit" value="提交" name="submit"><br/></form>
php基礎教程——表單驗證(必填、提交後資料保留)