php校验表单检测字段是否为空的方法

yipeiwu_com6年前PHP代码库

本文实例讲述了php校验表单检测字段是否为空的方法。分享给大家供大家参考。具体如下:

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.

希望本文所述对大家的php程序设计有所帮助。

相关文章

php json转换成数组形式代码分享

写的json转换成数组的一个类和方法,实际上写的方法可以把大部分包含json字符串的数据结构转换成数组,上代码: 复制代码 代码如下: class antiTranJson { &nbs...

php 上传功能实例代码

1.上传表单 upload.html 复制代码 代码如下: <form enctype="multipart/form-data" action="upload.php" meth...

PHP简单生成缩略图相册的方法

本文实例讲述了PHP简单生成缩略图相册的方法。分享给大家供大家参考。具体如下: <?php /* * written by mot * 根目录下自己新建image...

PHP获取文件后缀名的三个函数

选好一种记住,以后需要使用的时候就可以直接使用,或者来本站查看本文也是可以滴。 复制代码 代码如下: <?php //方法一: function extend_1($file_na...

PHP中list方法用法示例

本文实例讲述了PHP中list方法用法。分享给大家供大家参考,具体如下: <?php function small_numbers() { return array...