Northern Flame Posted November 4, 2007 Share Posted November 4, 2007 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(); } ?> Quote Link to comment Share on other sites More sharing options...
marcus Posted November 4, 2007 Share Posted November 4, 2007 Do something like: $errors = array(); //for each error you have name it $errors[] $errors[] = "first error"; $errors[] = "second error"; $errors[] = "third error"; foreach($errors AS $error){ echo $error . "<br>\n"; } Quote Link to comment Share on other sites More sharing options...
Northern Flame Posted November 4, 2007 Author Share Posted November 4, 2007 I added the $errors = array(); part but im still getting the error Quote Link to comment Share on other sites More sharing options...
marcus Posted November 4, 2007 Share Posted November 4, 2007 Ah, you're calling it inside a function. after you open the function add this: global $errors; Quote Link to comment Share on other sites More sharing options...
Northern Flame Posted November 4, 2007 Author Share Posted November 4, 2007 so should it be function pageContent(){ global $errors; ...... } or pageContent(); global $errors; Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted November 4, 2007 Share Posted November 4, 2007 The first way you said function pageContent(){ global $errors; ...... } Quote Link to comment Share on other sites More sharing options...
Northern Flame Posted November 4, 2007 Author Share Posted November 4, 2007 THANKS! Quote Link to comment 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.