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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.