Home >Backend Development >PHP Tutorial >PHP uses validation form to detect whether fields are empty

PHP uses validation form to detect whether fields are empty

墨辰丷
墨辰丷Original
2018-06-12 13:38:431321browse

这篇文章主要介绍了php校验表单检测字段是否为空的方法,涉及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[&#39;nickname&#39;])) {
   echo "<br /><b>Nickname</b> is required.";
   $errorcount++;
 }
 if (!trim($_POST[&#39;title&#39;])) {
   echo "<br /><b>Title</b> is required.";
   $errorcount++;
 }
 if ($errors > 0)
   echo "<br /><br />Please use your browser&#39;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基于cookie实现记录用户名和密码

PHP中使用生成的公钥、私钥进行加密解密的方法

The above is the detailed content of PHP uses validation form to detect whether fields are empty. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn