Home  >  Article  >  Backend Development  >  How to use the empty() function to determine whether the form text box input is empty?

How to use the empty() function to determine whether the form text box input is empty?

怪我咯
怪我咯Original
2017-04-28 17:03:017277browse

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.

How to use the empty() function to determine whether the form text box input is empty?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 example

Use 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>
                密&#12288码:<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:

How to use the empty() function to determine whether the form text box input is empty? Code for server-side verification using the empty() function:

<?php
header("content-type:text/html;charset=utf-8");

$user=$_POST[&#39;user&#39;];
$password=$_POST[&#39;password&#39;];
if(empty($user) || empty($password[&#39;password&#39;])){
    echo "用户名或密码不能为空<br/> <a href=&#39;login.html&#39;>返回</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:

How to use the empty() function to determine whether the form text box input is empty?#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!

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