龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

表单验证 php校验表单,检测字段是否为空

时间:2014-06-02 15:24来源:网络整理 作者:网络 点击:
分享到:
php校验表单,检测字段是否为空 [代码片段(22行)] php后端代码,保存为: ErrorCheck.php;
<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()函数可以去除字符串中的前后空字符
” ” (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.

精彩图集

赞助商链接