Jump to content

[SOLVED] Foreach error


Northern Flame

Recommended Posts

I created a contact form, which I have created before in almost the exact way,

but this time I'm getting an error, the error reads:

 

Warning: Invalid argument supplied for foreach() in /path/to/my/website.com/contact.php on line 40

 

Heres the script (line 40 is the line that has "foreach()" and the function pageContent() is what is displayed on the page):

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];

if(empty($name)) $errors[1] = "You forgot to fill out your name!";
if(empty($email)) $errors[2] = "You forgot to fill out your email address!";
if(empty($subject)) $errors[3] = "You forgot to fill out the subject!";
if(empty($message)) $errors[4] = "You forgot to fill out the message!";

if(!empty($errors)){
function pageContent(){
?>
<div id=content1>
			<h2>You had the following errors!</h2>
<?php 
foreach($errors as $key => $val){
echo '				<i>-'.$val.'</i><br>'."\n";
}
?>
			<form action="/contact.php" method="POST">
				Name:<br>
				<input type="text" name="name" size="20" value="<?php echo $name; ?>"><br>
				Email:<br>
				<input type="text" name="email" size="20" value="<?php echo $email; ?>"><br>
				Subject:<br>
				<input type="subject" name="subject" size="20" value="<?php echo $subject; ?>"><br>
				Message:<br>
				<textarea name="message" rows="7" cols="30"><?php echo $message; ?></textarea><br>
				<input type="submit" name="submit" value="Send">
			</form>
		</div>
<?php
}
include 'template/index.php';
exit();
}
?>

Link to comment
https://forums.phpfreaks.com/topic/75967-solved-foreach-error/
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.