I'm receiving the fooling error below but not sure why. the $_Post all have data.
error: Warning: Invalid argument supplied for foreach() in /home/soc/process_member.php on line 19
$valid = true;
foreach($_POST['add'] && $_POST['cpl'] as $value) {
if(!isset($value)) {
$valid = false;
}
}
if(!$valid) {
$err .= 'please fill in all fields';
$en['reg_error'] = '<div class=err style="color: #FF0000;"><b>'._error.': '.$err.'</b></div><br>';
load_template('modules/members/templates/s_reg.tpl');
}
I think what you want is a merged array:
foreach(array_merge($_POST['add'], $_POST['cpl']) as $value) {
// ...
First, in PHP, result of &&
operation is always boolean (i.e., true
or false
) - and you cannot supply it into foreach
(and why you may wish to?), hence the error.
Second, even if it were possible, it'd still make little sense to use isset
here: if either $_POST['add']
or $_POST['cpl']
is indeed unset, the notice is raised.
So, you can either just rewrite the first section of your code like this:
$valid = isset($_POST['add']) && isset($_POST['cpl']);
... or, taking into account the flexible nature of isset
construct, just...
$valid = isset($_POST['add'], $_POST['cpl']);
... because, as said in its' official doc:
If multiple parameters are supplied then isset() will return TRUE only if all of the parameters are set. Evaluation goes from left to right and stops as soon as an unset variable is encountered.
If there's actually a whole big set of args to check and/or you're too lazy to type '$_POST' each time, use foreach
to go through the array of their names (and not the actual values!) instead:
$valid = true;
foreach (array('add', 'cpl', 'whatever', 'it', 'takes') as $argName) {
if (! isset($_POST[$argName])) {
$valid = false;
break;
}
}
You should read more about PHP. There are two syntaxes for "foreach":
foreach (array_expression as $value)
statement
foreach (array_expression as $key => $value)
statement
This is totally wrong:
foreach($_POST['add'] && $_POST['cpl'] as $value) {
Let me give you an example:
<?php
foreach (array(1, 2, 3, 4) as &$value) {
$value = $value * 2;
}
?>
For more info, check the official PHP Documentation regarding foreach: http://php.net/manual/en/control-structures.foreach.php