这篇文章主要介绍了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['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基于cookie实现记录用户名和密码
php中使用生成的公钥、私钥进行加密解密的方法
以上就是php利用校验表单检测字段是否为空的详细内容。