嘿伙计们我正在尝试制作一个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!");
}
?>
以下是我的表单代码:
<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>
随着它总是返回“缺少信息!”即使填写了所有文本框。
我做错了什么?
答案 0 :(得分:6)
改为使用empty()
,
if (empty($dbname) || empty($dbuser) || empty($dbpass) || empty($username) || empty($password)) {
//Empty
}
else {
//Not Empty
}
检查输入的 name
是否正确。
答案 1 :(得分:0)
要进行调试,请尝试执行
print_r($_POST);
查看您在$ _POST变量中实际收到的内容,然后在if语句中一次尝试一次比较。 也许它会让你看到幕后。
答案 2 :(得分:0)
替换此&amp;检查是否获得所有参数值。
<?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!");
}
?>
答案 3 :(得分:0)
要检查发布的数据,您可以使用isset()
。
确定变量是否已设置且不是NULL
if(!isset($dbname) || trim($dbname) == '')
{
echo "You did not fill out the required fields.";
}
trim()
关键字删除empty()
忽略的空格。