I have a foreach loop to check if there is any empty post but i get 3x "empty" i only want to return 1 message if any post data is empty I have this PHP code.
if (isset($_POST["registreer"]))
{
unset($_POST["registreer"]);
foreach ($_POST as $input => $value)
{
if (empty($_POST[$input]))
{
echo "empty";
}
}
}
You won't need foreach
, instead you can use in_array()
if (in_array("", $_POST)) {
echo 'Whatever';
}
Use break
to end the loop after printing the message.
foreach ($_POST as $input => $value)
{
if (empty($value))
{
echo "empty";
break;
}
}
The below will add the empty $_POST arrays to the $emp array and print them after the loop. You didn't say if you want to know which ones are empty, but hopefully it helps.
if (isset($_POST["registreer"]))
{
unset($_POST["registreer"]);
foreach ($_POST as $input => $value)
{
if (empty($_POST[$input]))
{
$emp[$input] = "leeg";
}
}
print_r($emp);
}
Try This
if (isset($_POST["registreer"]))
{
unset($_POST["registreer"]);
$flag =0;
foreach ($_POST as $input => $value)
{
if (empty($_POST[$input]))
{
$flag = 1;
}
}
if( $flag==1){
echo "leeg";
}
}
This is return only one time "leeg" when post is empty
Try it like this, If you want to display message only once, wither empty field is one or more.
$errorSent = 0;
if (isset($_POST["registreer"]))
{
unset($_POST["registreer"]);
foreach ($_POST as $input => $value)
{
if (empty($_POST[$input]))
{
if ($errorSent == 0)
{
echo "empty";
}
$errorSent = 1
}
}
}