php校驗(yàn)表單檢測(cè)字段是否為空的方法

字號(hào):


    這篇文章主要介紹了php校驗(yàn)表單檢測(cè)字段是否為空的方法,涉及php驗(yàn)證表單的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    本文實(shí)例講述了php校驗(yàn)表單檢測(cè)字段是否為空的方法。分享給大家供大家參考。具體如下:
    php校驗(yàn)表單,檢測(cè)字段是否為空,當(dāng)表單中有未填寫的字段,則會(huì)顯示錯(cuò)誤信息。
    <html>
    <body>
    <form METHOD="POST" ACTION="ErrorCheck.php">
    <h1>Contact Information</h1>
    <label>Nickname:</label>
    <input TYPE="TEXT" NAME="nickname">
    <label>Title:</label>
    <input TYPE="TEXT" NAME="title">
    <br />
    <input TYPE="SUBMIT" VALUE="Submit">
    <br />
    <input TYPE="RESET" VALUE="Clear the Form">
    </form>
    </body>
    </html>
    php后端代碼,保存為: ErrorCheck.php
    <html>
    <body>
    <?php
    $errorcount=0;
    if (!trim($_POST['nickname'])) {
    echo "<br /><b>Nickname</b> is required.";
    $errorcount++;
    }
    if (!trim($_POST['title'])) {
    echo "<br /><b>Title</b> is required.";
    $errorcount++;
    }
    if ($errors > 0)
    echo "<br /><br />Please use your browser's back button " .
    "to return to the form, and correct error(s)";
    ?>
    </body>
    </html>
    trim()函數(shù)可以去除字符串中的前后空字符
    " " (ASCII 32 (0×20)), an ordinary space.
    "\t" (ASCII 9 (0×09)), a tab.
    "\n" (ASCII 10 (0x0A)), a new line (line feed).
    "\r" (ASCII 13 (0x0D)), a carriage return.
    "\0″ (ASCII 0 (0×00)), the NUL-byte.
    "\x0B" (ASCII 11 (0x0B)), a vertical tab.
    希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。