php表單驗(yàn)證——必填字段

示例

包含以下輸入字段: 必須與可選文本字段,單選按鈕,及提交按鈕:
首先是簡單的表單代碼:

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    名字: <input type="text" name="name"><br/><br/>
    E-mail: <input type="text" name="email"><br/><br/>
    網(wǎng)址: <input type="text" name="website"><br/><br/>
    備注: <textarea name="comment" rows="5" cols="40"></textarea><br/><br/>

    性別:<input type="radio" name="gender" value="female">女
          <input type="radio" name="gender" value="male">男
    <br/><br/>
    <input type="submit" name="submit" value="Submit">
</form>

注意:action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"
這里是為了避免 CSS (Cross-Site Script) 跨站腳本攻擊。

當(dāng)用戶提交表單時(shí),我們將做以下兩件事情:
1、使用 PHP trim() 函數(shù)去除用戶輸入數(shù)據(jù)中不必要的字符 (如:空格,tab,換行)。
2、使用PHP stripslashes()函數(shù)去除用戶輸入數(shù)據(jù)中的反斜杠 ()
將這些過濾的函數(shù)寫在一個(gè)我們自己定義的函數(shù)中,這樣可以大大提高代碼的復(fù)用性,對(duì)上面的表單代碼進(jìn)行修改:

<html>
<body>
<?php
// 定義變量并默認(rèn)設(shè)置為空值
$name = $email = $gender = $comment = $website = "";
 
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
  $name = test_input($_POST["name"]);
  $email = test_input($_POST["email"]);
  $website = test_input($_POST["website"]);
  $comment = test_input($_POST["comment"]);
  $gender = test_input($_POST["gender"]);
}
 
function test_input($data)
{
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    名字: <input type="text" name="name"><br/><br/>
    E-mail: <input type="text" name="email"><br/><br/>
    網(wǎng)址: <input type="text" name="website"><br/><br/>
    備注: <textarea name="comment" rows="5" cols="40"></textarea><br/><br/>

    性別:<input type="radio" name="gender" value="female">女
          <input type="radio" name="gender" value="male">男
    <br/><br/>
    <input type="submit" name="submit" value="Submit">
</form>

<?php
echo "<h2>您輸入的內(nèi)容是:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
?>
</body>
</html>
PHP - 必需字段

"名字", "E-mail", 和 "性別" 字段是必需的,各字段不能為空,網(wǎng)址可選, 如果存在,它必需包含一個(gè)有效的URL。
修改上面的代碼:

<html>
<body>
<?php
// 定義變量并默認(rèn)設(shè)置為空值
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";
 
if ($_SERVER["REQUEST_METHOD"] == "POST"){
    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["comment"])){
        $comment = "";
    }else{
        $comment = test_input($_POST["comment"]);
    }
    
    if (empty($_POST["gender"])){
        $genderErr="性別是必須的。";
    }else{
        $gender = test_input($_POST["gender"]);
    }

}
 
function test_input($data)
{
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>
<style>
    .error
    { text-align:"center";color:red;}
</style>

<p><span class="error">* 必填字段。</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    名字: <input type="text" name="name">
    <span class="error">* <?php echo $nameErr;?></span>
    <br/><br/>
    E-mail: <input type="text" name="email">
    <span class="error">* <?php echo $emailErr;?></span>
    <br/><br/>
    網(wǎng)址: <input type="text" name="website">
    <span class="error"><?php echo $websiteErr;?></span>
    <br/><br/>
    備注: <textarea name="comment" rows="5" cols="40"></textarea><br/><br/>

    性別:<input type="radio" name="gender" value="female">女
          <input type="radio" name="gender" value="male">男
         <span class="error">* <?php echo $genderErr;?></span>
    <br/><br/>
    <input type="submit" name="submit" value="Submit">
</form>

<?php
echo "<h2>您輸入的內(nèi)容是:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
?>
</body>
</html>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • PHP - 輸入字段 從上一節(jié)中的驗(yàn)證規(guī)則中,我們看到 "Name", "E-mail" 以及 "Gender" ...
    林路同閱讀 1,924評(píng)論 0 1
  • PHP 表單驗(yàn)證:在HTML表單提交的的時(shí)候,經(jīng)常會(huì)有關(guān)鍵字段需要我們做安全驗(yàn)證,這樣對(duì)于防范黑客和垃圾郵件很重要...
    林路同閱讀 429評(píng)論 0 6
  • 什么是 $_SERVER["PHP_SELF"] 變量? $_SERVER["PHP_SELF"] 是一種超全局變...
    幸宇閱讀 789評(píng)論 1 1
  • 漸變的面目拼圖要我怎么拼? 我是疲乏了還是投降了? 不是不允許自己墜落, 我沒有滴水不進(jìn)的保護(hù)膜。 就是害怕變得面...
    悶熱當(dāng)乘涼閱讀 4,495評(píng)論 0 13
  • 感覺自己有點(diǎn)神經(jīng)衰弱,總是覺得手機(jī)響了;屋外有人走過;每次媽媽不聲不響的進(jìn)房間突然跟我說話,我都會(huì)被嚇得半死!一整...
    章魚的擁抱閱讀 2,404評(píng)論 4 5

友情鏈接更多精彩內(nèi)容