Jump to content

foreach() error


gerbs987

Recommended Posts

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";
}

 

 

Link to comment
https://forums.phpfreaks.com/topic/70062-foreach-error/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/70062-foreach-error/#findComment-351836
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/70062-foreach-error/#findComment-351920
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.