gerbs987 Posted September 20, 2007 Share Posted September 20, 2007 Hey, i'm a php newbie, and i'm trying to do some simple form field validation and i keep getting the following error: Warning: Invalid argument supplied for foreach() I'm not sure what the problem is, but if anyone can help, i'd appreciate it. The following is where the error is happening. I can post all of it if needed. foreach($loler as $key => $value){ $string .= $value."\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/70062-foreach-error/ Share on other sites More sharing options...
remlabm Posted September 20, 2007 Share Posted September 20, 2007 $loler is not a correct array format... do a this: echo "<pre>"; print_r($loler); echo "</pre>"; and post the results back Quote Link to comment https://forums.phpfreaks.com/topic/70062-foreach-error/#findComment-351831 Share on other sites More sharing options...
gerbs987 Posted September 20, 2007 Author Share Posted September 20, 2007 OK. i'm not totally sure on using the code above, i think i'm totally way off base with what i'm doing. I have my functions displayed here <?php extract($_POST); function check_33($field1){ if(!preg_match("/[^0-9]+$/ ",$field1)) return TRUE; else return FALSE; } function check_34($field2){ if(!preg_match("/[^a-zA-Z0-9]+$/s",$field2)) return TRUE; else return FALSE; } function show_form() { ?> and then the rest of my code is here <?php foreach($_POST as $key => $value){ $loler[$key] = $value; } if(sizeof($array) > 3){ echo "only select 1 please"; show_form(); exit; } $error=0; if(!check_33(33)){ echo "You must enter total points for MNF!!!"; $error++; show_form(); exit; } if(!check_34(34)){ echo "You must enter your screen name!!!"; $error++; show_form(); exit; } $to = "@.com"; $subject = $_POST['34']; foreach($loler as $key => $value){ $string .= $value."\n"; } $headers = "From:@com"; mail($to, $subject, $headers, $string); echo '<h2><b><font face="arial">Thanks for submitting your picks! Please double check below, or print for your own records!</font></b></h2>'; echo '<b><font face="arial"><center>' .$string. '</center></b></font>'; } if(!isset($_POST['34'])){ show_form(); exit; } ?> I've just been taking snippets and placing where i need, but i think it might be way off. Quote Link to comment https://forums.phpfreaks.com/topic/70062-foreach-error/#findComment-351836 Share on other sites More sharing options...
JJohnsenDK Posted September 20, 2007 Share Posted September 20, 2007 this code: foreach($_POST as $key => $value){ $loler[$key] = $value; } where do you define your $_POST? Quote Link to comment https://forums.phpfreaks.com/topic/70062-foreach-error/#findComment-351839 Share on other sites More sharing options...
gerbs987 Posted September 20, 2007 Author Share Posted September 20, 2007 <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> Quote Link to comment https://forums.phpfreaks.com/topic/70062-foreach-error/#findComment-351840 Share on other sites More sharing options...
JJohnsenDK Posted September 20, 2007 Share Posted September 20, 2007 allright... but are the code you are showing in two different files? if so then move this: extract($_POST); into the ohter document Quote Link to comment https://forums.phpfreaks.com/topic/70062-foreach-error/#findComment-351846 Share on other sites More sharing options...
gerbs987 Posted September 20, 2007 Author Share Posted September 20, 2007 oh. No, all the code is in the same file. Quote Link to comment https://forums.phpfreaks.com/topic/70062-foreach-error/#findComment-351847 Share on other sites More sharing options...
gerbs987 Posted September 20, 2007 Author Share Posted September 20, 2007 ok, all that takes care of the error, but now my echo statement after submitting is displaying @ the bottom, and the validation doesn't work. Quote Link to comment https://forums.phpfreaks.com/topic/70062-foreach-error/#findComment-351849 Share on other sites More sharing options...
JJohnsenDK Posted September 20, 2007 Share Posted September 20, 2007 which echo statement is showing the @... and what took care of the error? Quote Link to comment https://forums.phpfreaks.com/topic/70062-foreach-error/#findComment-351869 Share on other sites More sharing options...
gerbs987 Posted September 20, 2007 Author Share Posted September 20, 2007 the echo statement that is showing at the bottom of the page are the 2 located below: $to = "@.com"; $subject = $_POST['34']; foreach($loler as $key => $value){ $string .= $value."\n"; } $headers = "From:@com"; mail($to, $subject, $headers, $string); echo '<h2><b><font face="arial">Thanks for submitting your picks! Please double check below, or print for your own records!</font></b></h2>'; echo '<b><font face="arial"><center>' .$string. '</center></b></font>'; } if(!isset($_POST['34'])){ show_form(); exit; } ?> and the $_POST in the foreach statement fixed the error. Quote Link to comment https://forums.phpfreaks.com/topic/70062-foreach-error/#findComment-351920 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.