Is there a simpler function to something like this:
if [isset[$_POST['Submit']]] {
if [$_POST['login'] == "" || $_POST['password'] == "" || $_POST['confirm'] == "" || $_POST['name'] == "" || $_POST['phone'] == "" || $_POST['email'] == ""] {
echo "error: all fields are required";
} else {
echo "proceed...";
}
}
Taryn
238k55 gold badges362 silver badges402 bronze badges
asked Jul 6, 2010 at 21:42
Something like this:
// Required field names
$required = array['login', 'password', 'confirm', 'name', 'phone', 'email'];
// Loop over field names, make sure each one exists and is not empty
$error = false;
foreach[$required as $field] {
if [empty[$_POST[$field]]] {
$error = true;
}
}
if [$error] {
echo "All fields are required.";
} else {
echo "Proceed...";
}
answered Jul 6, 2010 at 21:46
Harold1983-Harold1983-
3,2292 gold badges22 silver badges22 bronze badges
8
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
}
answered Sep 18, 2012 at 21:54
Nahser BakhtNahser Bakht
9102 gold badges13 silver badges26 bronze badges
2
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.
answered Jul 6, 2010 at 21:50
animuson♦animuson
52.7k28 gold badges139 silver badges145 bronze badges
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...
answered Jul 6, 2010 at 21:44
Jacob RelkinJacob Relkin
158k32 gold badges341 silver badges318 bronze badges
2
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
}
answered Nov 4, 2013 at 0:03
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...
}
answered Feb 3, 2015 at 0:41
user1518699user1518699
3872 gold badges8 silver badges19 bronze badges
Note : Just be careful if 0 is an acceptable value for a required field. As @Harold1983- mentioned, these are treated as empty in PHP. For these kind of things we should use isset instead of empty.
$requestArr = $_POST['data']// Requested data
$requiredFields = ['emailType', 'emailSubtype'];
$missigFields = $this->checkRequiredFields[$requiredFields, $requestArr];
if [$missigFields] {
$errorMsg = 'Following parmeters are mandatory: ' . $missigFields;
return $errorMsg;
}
// Function to check whether the required params is exists in the array or not.
private function checkRequiredFields[$requiredFields, $requestArr] {
$missigFields = [];
// Loop over the required fields and check whether the value is exist or not in the request params.
foreach [$requiredFields as $field] {`enter code here`
if [empty[$requestArr[$field]]] {
array_push[$missigFields, $field];
}
}
$missigFields = implode[', ', $missigFields];
return $missigFields;
}
answered Dec 27, 2017 at 9:22
foreach[$_POST as $key=>$value]
{
if[empty[trim[$value]]
echo "$key input required of value ";
}
answered Aug 13, 2019 at 18:21
dılo sürücüdılo sürücü
3,1581 gold badge17 silver badges26 bronze badges
5
Personally I extract the POST array and then have if[!$login || !$password] then echo fill out the form :]
answered Jul 6, 2010 at 21:45
Doug MolineuxDoug Molineux
12.1k25 gold badges89 silver badges142 bronze badges
2