How to determine if the form is not empty in php

藏色散人
Release: 2023-03-10 08:00:01
Original
1567 people have browsed it

php判断表单不为空的方法:首先创建一个php校验表单,并检测字段是否为空;然后创建后端文件“ErrorCheck.php”;最后通过trim函数去除字符串中的前后空字符即可。

How to determine if the form is not empty in php

本文操作环境:windows7系统、PHP7.1版,DELL G3电脑

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>
Copy after login

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>
Copy after login

trim()函数可以去除字符串中的前后空字符

推荐学习:《PHP视频教程

" " (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.
Copy after login

The above is the detailed content of How to determine if the form is not empty in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!