Jump to content

[SOLVED] Post and valadation.


redarrow

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.