Jump to content

Recommended Posts

advance thank you.

 

if i got a huge form and i want to make a condition using $_POST and

only let it update the database if there no $warning.

 

is it ok using it this way or not.

 

<?php
foreach($_POST as $key=>$val){

if(empty($val)){

echo"<div style='font-size: 36; color: #FF0000; text-align: center;'> PLEASE USE ALL THE FORM!</div>";

$warning="stop";

break;

}
}	

if(!$warning){

//do somethink

}
?>

Link to comment
https://forums.phpfreaks.com/topic/150874-solved-post-and-valadation/
Share on other sites

Something like this would probably be better:

 

$requiredFields = array('field1', 'field2', 'field3' /* etc. */);
$missingFields = array();

foreach ($requiredFields as $field) {
if (!isset($_POST[$field]) || empty($_POST[$field])) {
	$missingFields[] = $field;
}
}

if (count($missingFields)) {
echo 'Please fill out the remaining fields: ' . join($missingFields, ', ');
// show form
}
else {
// process form
}

 

That'll be more user friendly seeing as you give feedback about which fields they are missing. This will be especially helpful considering that the form is "huge". It would also allow you to have optional fields should you decide that at a later point.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.