Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PHP Help, foreach
#1
that is the warning i get from this code,


Code:
<?php
if (empty($_POST) === false) {
    $errors = array();
    
    $name         = $_POST['name'];
    $email          = $_POST['email'];
    $message             = $_POST['message'];
    
    if (empty($name) === true || empty($email) === true || empty($message) === true) {
        $errors = 'Name, Email and Message are required!';    
    } else {
        if(filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
            $errors = 'That\'s not a valid email address';
        }
        if (ctype_alpha($name) === false) {
        $errors = 'Name must only contain letters!';
        }    
    }
    
    if(empty($errors) === true) {
        mail('*******@hotmail.co.uk', 'Contact Form', $message, 'From:' . $email);
        header('Location: index.php?sent');
        exit();
    
    }
}
?>
<DOCTYPE html>
<body>
<?php
        if(isset($_GET['sent']) === true) {
            echo '<p>Thanks for contacting us!</p>';
            } else {
        if(empty($errors) === false) {
            }echo '<ul>';
            {
"line 77"                foreach($errors as $error)
                echo'<li>', $errors , '</li>';
            }
            echo'</ul>';
            }
        {
        
    ?>
    <form method="post">
        <p>
            <label for="name">Name:</label><br/>
            <input type="text" name="name" id="name" <?php if(isset($_POST['name']) === true) { echo 'value="', strip_tags($_POST['name']), '"';} ?>>
        </p>
        <p>
            <label for="email">Email:</label><br/>
            <input type="text" name="email" id="email" <?php if(isset($_POST['email']) === true) { echo 'value="', strip_tags($_POST['email']), '"';} ?>>
        </p>
        <p>
            <label for="message">Message:</label><br/>
            <textarea name="message" id="message"><?php if(isset($_POST['message']) === true) { echo strip_tags($_POST['message']);} ?></textarea>
        </p>
        <p>
            <input type="submit" value="Submit" />
        </p>
        
    </form>
    <?php
        }
    ?>

</body>
Warning: Invalid argument supplied for foreach() in /home/*****/public_html/test/contact1.php on line 77

i was just trying to make a contact form for my website and that error keeps appearing !

could anyone help me please thanks Smile
Reply
#2
The problem is you're attempting to use the foreach construct upon a string. Even though you assigned the $errors variable to an empty array (and therefore type-casting it to an array), PHP's loose typing enables us to type-juggle our variables. This is exactly what you're doing by assigning the $errors variable to a string. You'll need to change how you're assigning the errors if you'd like to fix your problem:
PHP Code:
$errors[] = 'error here';
$errors[] = 'error2 here'

Because we've used the square brackets upon the $errors variable, we're now inserting a new element into our array, rather than type-juggling it to a string with only one value.
Reply
#3
Add a

Code:
{

after:

Code:
as $error)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PHP Framework List: An Ultimate Guide to 102 PHP Frameworks for Web Developers tk-hassan 0 943 07-27-2020, 11:26 PM
Last Post: tk-hassan
  PHP Video Tutorials (PHP For Beginners) Eleqtriq 4 3,671 10-10-2011, 01:00 PM
Last Post: Greyersting

Forum Jump:


Users browsing this thread: 1 Guest(s)