显示';所有必填字段';使用PHP时出错(默认情况下隐藏)
我想知道如何在表单提交后显示“所有字段必填”通知,但在提交前将其隐藏。这是我的密码:显示';所有必填字段';使用PHP时出错(默认情况下隐藏),php,Php,我想知道如何在表单提交后显示“所有字段必填”通知,但在提交前将其隐藏。这是我的密码: if (isset($_POST['region'], $_POST['description'], $_POST['remarks']) && !empty ($_POST['region']) && !empty($_POST['description']) && !empty($_POST['remarks']) ) { $region = m
if (isset($_POST['region'], $_POST['description'], $_POST['remarks']) && !empty ($_POST['region']) && !empty($_POST['description']) && !empty($_POST['remarks']) )
{
$region = mysql_escape_string($_POST['region']);
$description = mysql_escape_string($_POST['description']);
$remarks = mysql_escape_string($_POST['remarks']);
if (strlen($region>50)) {
$error .= "Only 50 characters are allowed in the region field.";
}
else {
if (strlen($description>500)) {
$error .= "Only 500 characters are allowed in the region field.";
}
else {
mysql_query("INSERT INTO register (region, description, remarks) VALUES(
'$region',
'$description',
'$remarks') ") or die('Not saved. ' . mysql_error());
}
}
}
else {
$error.= "Please fill all the fields.";
}
echo $error;
?>
//form starts here
但是,使用此选项会显示“请填写所有字段”,即使页面是第一次加载(不提交输入)。我该怎么办?此外,即使嵌套的“if”也不起作用。这些字符串仍然可以提交:(检查用户请求是POST还是GET 通过简单地检查是否设置了$\u POST,最容易实现
/* ... */
else if (isset($_POST)) {
$error.= "Please fill all the fields.";
}
我很确定你可以通过检查
$\u服务器['REQUEST\u METHOD']='POST'
来达到同样的效果。可能会更干净一些。注意:你应该考虑使用mysqli.*
而不是mysql.*
,因为mysql函数很快就会被弃用。这真的很奇怪。我的意思是你应该只使用isset($\u POST['BLAH'))
控制,但您的书写也是正确的。您能检查一下$\u POST['comments']
、$\u POST['description']
和$\u POST['region']的值吗
在else
语句中。可能它们对服务器来说是零,并且!empty
执行为False。这都是关于empty
;)