Support Forums
PHP Help, foreach - Printable Version

+- Support Forums (https://www.supportforums.net)
+-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87)
+--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18)
+---- Forum: PHP The Hypertext Preprocessor (https://www.supportforums.net/forumdisplay.php?fid=21)
+---- Thread: PHP Help, foreach (/showthread.php?tid=27467)



PHP Help, foreach - Tubby - 12-09-2012

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


RE: PHP Help, foreach - Haxalot - 03-18-2013

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.


RE: PHP Help, foreach - Excuse - 07-02-2013

Add a

Code:
{

after:

Code:
as $error)