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(); } ?> Link to comment https://forums.phpfreaks.com/topic/75967-solved-foreach-error/ 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"; } Link to comment https://forums.phpfreaks.com/topic/75967-solved-foreach-error/#findComment-384548 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 Link to comment https://forums.phpfreaks.com/topic/75967-solved-foreach-error/#findComment-384550 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; Link to comment https://forums.phpfreaks.com/topic/75967-solved-foreach-error/#findComment-384554 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; Link to comment https://forums.phpfreaks.com/topic/75967-solved-foreach-error/#findComment-384557 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; ...... } Link to comment https://forums.phpfreaks.com/topic/75967-solved-foreach-error/#findComment-384559 Share on other sites More sharing options...
Northern Flame Posted November 4, 2007 Author Share Posted November 4, 2007 THANKS! Link to comment https://forums.phpfreaks.com/topic/75967-solved-foreach-error/#findComment-384562 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.