simcoweb Posted April 17, 2007 Share Posted April 17, 2007 I'm trying to validate the fields in a simple contact form (about 6 fields) and using this code to display the errors: <div align='center'><font color='red'><strong id='errorTitle'><?= !empty($eg_error) ? 'One or more input fields on the form has not been correctly completed.' : '' ?></strong> <? // Loop through all errors if(!empty($err)) { ?> <ul> <? foreach($err as $value) { ?> <li id='errorMess'><? echo $value ?></li> <? } ?> </ul> <? } ?> And using this to check the fields: <? // input error checking if ($name=="") { $err.= "Please enter your name.<br/>"; } if (!$phone==""){ $err.= "Please enter your phone number.<br/>"; } if (!$email) { $err.= "Please provide your email address<br>"; } if ($email) { if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) { $err.= $email. " is not a valid email address.<br/>"; } } /* if (!$secure) { $err.= "No security code entered<br/>"; } if (($secure!=$match) && ($secure!="")) { $err.= "Security code mismatch. Please re-enter.<br/>"; } */ if ($err=="") { // mail the results to admin send_ebook_mail(); // run the query ebook_insert(); } ?> I get this error: Warning: Invalid argument supplied for foreach() in /home/content/x/y/z/bbbaley8076/html/ebook.php on line 110 Which is, of course, the foreach statement line. Help? Link to comment https://forums.phpfreaks.com/topic/47431-solved-foreach-statement-saying-not-valid-argumentwhyyyyy/ Share on other sites More sharing options...
Psycho Posted April 17, 2007 Share Posted April 17, 2007 Where is $err set? Is it an array? Link to comment https://forums.phpfreaks.com/topic/47431-solved-foreach-statement-saying-not-valid-argumentwhyyyyy/#findComment-231455 Share on other sites More sharing options...
fert Posted April 17, 2007 Share Posted April 17, 2007 $err need to be an array Link to comment https://forums.phpfreaks.com/topic/47431-solved-foreach-statement-saying-not-valid-argumentwhyyyyy/#findComment-231456 Share on other sites More sharing options...
simcoweb Posted April 17, 2007 Author Share Posted April 17, 2007 First, thanks for the posts My logical mind tells me that $err is set IF there's an error in one of the fields being validated. In other words: if ($name=="") { $err.= "Please enter your name.<br/>"; Would that not set a value for $err? Link to comment https://forums.phpfreaks.com/topic/47431-solved-foreach-statement-saying-not-valid-argumentwhyyyyy/#findComment-231459 Share on other sites More sharing options...
HeyRay2 Posted April 17, 2007 Share Posted April 17, 2007 You are using $err as a string. foreach() requires an array. If you simply want echo out $err, you don't need to use foreach(), just do something like this: if(!empty($err)){ echo $err; } However, if you want to separate each error into a list using foreach(), then change your error checking to something like this: <?php // Create and empty error array $err = array(); // input error checking if ($name=="") { $err[] = "Please enter your name.<br/>"; } if (!$phone==""){ $err[] = "Please enter your phone number.<br/>"; } if (!$email) { $err[] = "Please provide your email address<br>"; } if ($email) { if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) { $err[] = $email. " is not a valid email address.<br/>"; } } /* if (!$secure) { $err[] = "No security code entered<br/>"; } if (($secure!=$match) && ($secure!="")) { $err[] = "Security code mismatch. Please re-enter.<br/>"; } */ if (empty($err)) { // mail the results to admin send_ebook_mail(); // run the query ebook_insert(); } else { ?> <div align='center'><font color='red'><strong id='errorTitle'><?= !empty($eg_error) ? 'One or more input fields on the form has not been correctly completed.' : '' ?></strong> <? // Loop through all errors ?> <ul> <? foreach($err as $value) { ?> <li id='errorMess'><? echo $value ?></li> <? } ?> </ul> <? } ?> Link to comment https://forums.phpfreaks.com/topic/47431-solved-foreach-statement-saying-not-valid-argumentwhyyyyy/#findComment-231462 Share on other sites More sharing options...
simcoweb Posted April 17, 2007 Author Share Posted April 17, 2007 Ok, got that worked out. Learn more each day! Thanks, guys. Link to comment https://forums.phpfreaks.com/topic/47431-solved-foreach-statement-saying-not-valid-argumentwhyyyyy/#findComment-231522 Share on other sites More sharing options...
MadTechie Posted April 17, 2007 Share Posted April 17, 2007 Please click solved! Link to comment https://forums.phpfreaks.com/topic/47431-solved-foreach-statement-saying-not-valid-argumentwhyyyyy/#findComment-231523 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.