I was wondering how I could display an 'all fields required' notice AFTER the form has been submitted but hide it before. Here's my code:
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
However, using this shows 'Please fill all the fields' even when the page is loaded for the first time (without submitting inputs). How do I go about this? Plus, even the nested 'if's aren't working. Those strings can still be submitted :(
check if user request is POST or GET.
It will be easiest to achieve by simply checking if $_POST is set.
/* ... */
else if (isset($_POST)) {
$error.= "Please fill all the fields.";
}
I'm pretty sure you can achieve the same by checking $_SERVER['REQUEST_METHOD'] == 'POST'
, may be a little bit cleaner.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.