php 检查文本框是否为空?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7049975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:53:25  来源:igfitidea点击:

Checking Whether a textbox is empty?

phptextbox

提问by Duncan Palmer

Hey guys i'm trying to make a php script check whether or not the text boxes are empty, i am using the below code.

嘿伙计们,我正在尝试使用 php 脚本检查文本框是否为空,我正在使用以下代码。

<?php
$dbname = $_POST['db_name'];
$dbuser = $_POST['db_user'];
$dbpass = $_POST['db_pass'];
$username = $_POST['username'];
$password = $_POST['password'];

if($dbname == "" || $dbuser == "" || $dbpass == "" || $username == "" || $password == "")
{
Echo("Missing Information!");
}else{
Echo("Success!");
}
?>

Below is my form code:

下面是我的表单代码:

    <form method="post" style="padding-left: 50px" action="install_submit.php">
<h2>Database connection settings</h2>
<label for="db_name">Database name:</label>
<input type="textbox" id="db_name" name="db_name" /><br/>
    <label for="db_user">Database username:</label>
    <input type="textbox" id="db_user" name="db_user" /><br/>
<label for="db_pass">Database password:</label>
<input type="password" id="db_pass" name="db_pass" /><br/>
<h2>CPanel settings</h2>
<label for="username">Username:</label>
<input type="textbox" id="username" name="username" /><br/>
<label for="password">Password:</label>
<input type="password" id="password" name="password" /><br/>


<input type="submit" name="submit_install" value="Install" />
</form>

With that it always returns "Missing Information!" even when all the text boxes are filled in.

有了它,它总是返回“缺少信息!” 即使所有的文本框都填满了。

What am i doing wrong?

我究竟做错了什么?

回答by Madara's Ghost

Use empty()instead,

使用empty()替代,

if (empty($dbname) || empty($dbuser) || empty($dbpass) || empty($username) || empty($password)) {
    //Empty
}
else {
    //Not Empty
}

Check to see that the namesof the inputs are correct as well.

检查输入的names是否也正确。

回答by broch

For debugging try to do a

对于调试尝试做一个

print_r($_POST);

to see what you actually receive in the $_POST variable and then try one comparison at a time in your if statement. Maybe it makes you see behind the scene.

查看您在 $_POST 变量中实际收到的内容,然后在 if 语句中一次尝试一个比较。也许它会让你看到幕后。

回答by jay.jivani

Replace this & check whether you get all parameters value or not.

替换它并检查您是否获得所有参数值。

<?php
$dbname = $_POST['db_name'];
$dbuser = $_POST['db_user'];
$dbpass = $_POST['db_pass'];
$username = $_POST['username'];
$password = $_POST['password'];

echo "<pre>";
print_r($_POST);
echo "</pre>";
die();

if($dbname == "" || $dbuser == "" || $dbpass == "" || $username == "" || $password == ""){
    echo("Missing Information!");
}else{
    echo("Success!");
}
?>

回答by Sanket

To check the posted data u can use isset().

要检查发布的数据,您可以使用isset()

Determine if a variable is set and is not NULL

确定变量是否已设置且不为 NULL

if(!isset($dbname) || trim($dbname) == '')
{
   echo "You did not fill out the required fields.";
} 

trim()keyword remove white spaces which are ignored by empty().

trim()关键字删除被忽略的空格empty()