In website program development, there are many web data submission pages. The simplest one is the login page. When we enter and submit content in the text box of the login page, it is not allowed to be empty. In addition to controlling the text box not to be empty through JavaScript scripts on the client, we can also make judgments on the server.
We generally verify the form once in the front end and once in the back end.
If you want to determine whether the value submitted by the form is empty on the server side, you can use the empty() function to check whether the variable is empty. Is empty, returns true if the variable is empty or zero. When the variables are: "", 0, "0", null, false, array(), var $var and objects without any attributes, they are considered empty. The syntax of the empty() function is as follows:
bool empty ( mixed $var )
The parameter var is the input variable
Application exampleUse the empty() function to verify login Whether the user name in the page is empty, the code is as follows,
The first is the login page code, the code is as follows:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <form action="login.php" method="post" onsubmit="return foo();" name="myform" > <div class="container"> <div class="right"> <h2>用户登陆</h2> <p> 用户名:<input type="text" name="user" id="name" > </p> <p> 密 码:<input type="password" name="password" id="pwd"> </p> <p><input type="hidden" value="hadden" name="hadden"></p> <p> <button class="button">立即登陆</button> </p> </div> </div> </form> </body> </html>
The code running result is as follows:
Code for server-side verification using the empty() function:
<?php header("content-type:text/html;charset=utf-8"); $user=$_POST['user']; $password=$_POST['password']; if(empty($user) || empty($password['password'])){ echo "用户名或密码不能为空<br/> <a href='login.html'>返回</a> "; }else{ echo "输入的用户名为 $user 密码为:$password"; } ?>
When the username and password text boxes are empty, click the Login Now button to verify the submitted form information , the running result is as follows:
#The above is a simple example where the empty() function checks to be empty. I hope everyone can understand it
The above is the detailed content of How to use the empty() function to determine whether the form text box input is empty?. For more information, please follow other related articles on the PHP Chinese website!