thefortrees Posted June 6, 2007 Share Posted June 6, 2007 The foreach loop is throwing the error "invalid argument supplied for foreach()" even though I defined the array right before it. Any ideas? $required = array ('email', 'password1', 'password2', 'fname', 'lname', 'phone', 'address1', 'city', 'state', 'zip', 'nameoncert'); function checkData(){ foreach($required as $index){ $value = $_POST[$index]; if (empty($value) ){ $msg = 'Please fill out all required fields.'; return $msg; } } if ($_POST['password1'] != $_POST['password2']){ $msg = 'Password does not match.'; return $msg; } if ( !checkEmailAddress($_POST['email']) ){ $msg = 'Invalid e-mail address.'; return $msg; } } Quote Link to comment https://forums.phpfreaks.com/topic/54458-solved-loop-within-a-function-problems/ Share on other sites More sharing options...
chigley Posted June 6, 2007 Share Posted June 6, 2007 <?php function checkData($required){ foreach($required as $index){ $value = $_POST[$index]; if (empty($value) ){ $msg = 'Please fill out all required fields.'; return $msg; } } if ($_POST['password1'] != $_POST['password2']){ $msg = 'Password does not match.'; return $msg; } if ( !checkEmailAddress($_POST['email']) ){ $msg = 'Invalid e-mail address.'; return $msg; } } $required = array ('email', 'password1', 'password2', 'fname', 'lname', 'phone', 'address1', 'city', 'state', 'zip', 'nameoncert'); $result = checkdata($required); ?> Alternatively you can do this: <?php $required = array ('email', 'password1', 'password2', 'fname', 'lname', 'phone', 'address1', 'city', 'state', 'zip', 'nameoncert'); function checkData(){ global $required; foreach($required as $index){ $value = $_POST[$index]; if (empty($value) ){ $msg = 'Please fill out all required fields.'; return $msg; } } if ($_POST['password1'] != $_POST['password2']){ $msg = 'Password does not match.'; return $msg; } if ( !checkEmailAddress($_POST['email']) ){ $msg = 'Invalid e-mail address.'; return $msg; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/54458-solved-loop-within-a-function-problems/#findComment-269346 Share on other sites More sharing options...
The Little Guy Posted June 6, 2007 Share Posted June 6, 2007 before the foreach loop, and in the function do this: global $required; Quote Link to comment https://forums.phpfreaks.com/topic/54458-solved-loop-within-a-function-problems/#findComment-269347 Share on other sites More sharing options...
thefortrees Posted June 6, 2007 Author Share Posted June 6, 2007 Ah yes, the good old PHP learning curve. Thanks both of you! Quote Link to comment https://forums.phpfreaks.com/topic/54458-solved-loop-within-a-function-problems/#findComment-269348 Share on other sites More sharing options...
kenrbnsn Posted June 6, 2007 Share Posted June 6, 2007 You have scope problems. Variables defined outside a function can not be seen inside a function. You can do 1 of three things here: Define the array inside the function <?php function checkData(){ $required = array ('email', 'password1', 'password2', 'fname', 'lname', 'phone', 'address1', 'city', 'state', 'zip', 'nameoncert'); ?> [*] Define the array as global inside the function: <?php function checkData(){ global $required; ?> [*]Pass the array as an argument to the function: <?php $required = array ('email', 'password1', 'password2', 'fname', 'lname', 'phone', 'address1', 'city', 'state', 'zip', 'nameoncert'); function checkData($required){ ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/54458-solved-loop-within-a-function-problems/#findComment-269351 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.