Is there a simpler function to something like this:
if (isset($_POST[\'Submit\'])) {
if ($_POST[\'login\'] == \"\" || $_POST[\'password\'] == \"\" || $_P
I did it like this:
$missing = array();
foreach ($_POST as $key => $value) { if ($value == "") { array_push($missing, $key);}}
if (count($missing) > 0) {
echo "Required fields found empty: ";
foreach ($missing as $k => $v) { echo $v." ";}
} else {
unset($missing);
// do your stuff here with the $_POST
}
empty and isset should do it.
if(!isset($_POST['submit'])) exit();
$vars = array('login', 'password','confirm', 'name', 'email', 'phone');
$verified = TRUE;
foreach($vars as $v) {
if(!isset($_POST[$v]) || empty($_POST[$v])) {
$verified = FALSE;
}
}
if(!$verified) {
//error here...
exit();
}
//process here...
I use my own custom function...
public function areNull() {
if (func_num_args() == 0) return false;
$arguments = func_get_args();
foreach ($arguments as $argument):
if (is_null($argument)) return true;
endforeach;
return false;
}
$var = areNull("username", "password", "etc");
I'm sure it can easily be changed for you scenario. Basically it returns true if any of the values are NULL, so you could change it to empty or whatever.
foreach($_POST as $key=>$value)
{
if(empty(trim($value))
echo "$key input required of value ";
}
I just wrote a quick function to do this. I needed it to handle many forms so I made it so it will accept a string separated by ','.
//function to make sure that all of the required fields of a post are sent. Returns True for error and False for NO error
//accepts a string that is then parsed by "," into an array. The array is then checked for empty values.
function errorPOSTEmpty($stringOfFields) {
$error = false;
if(!empty($stringOfFields)) {
// Required field names
$required = explode(',',$stringOfFields);
// Loop over field names
foreach($required as $field) {
// Make sure each one exists and is not empty
if (empty($_POST[$field])) {
$error = true;
// No need to continue loop if 1 is found.
break;
}
}
}
return $error;
}
So you can enter this function in your code, and handle errors on a per page basis.
$postError = errorPOSTEmpty('login,password,confirm,name,phone,email');
if ($postError === true) {
...error code...
} else {
...vars set goto POSTing code...
}
if( isset( $_POST['login'] ) && strlen( $_POST['login'] ))
{
// valid $_POST['login'] is set and its value is greater than zero
}
else
{
//error either $_POST['login'] is not set or $_POST['login'] is empty form field
}