Page 1 of 1

Registering

Posted: Tue Jul 05, 2011 8:47 pm
by JelvinJS7
<?php
if (isset($_POST['name'], $_POST['username'], $_POST['password'], $_POST['repeatpass'], $_POST['email'])){
            $name = $_POST['name'];
            $username   = $_POST['username'];
            $password   = $_POST['password'];
            $repeatpass = $_POST['repeatpass'];
            $email      = $_POST['email'];
            //set error conditions
            $errors = array();
            if ((empty($name) || empty($username) || empty($password) || empty($repeatpass) || empty($email) === true)){
                $errors[] = "Please fill in every field.";
            }
            if (strlen($username)>25 || strlen($username)<3){
                $errors[] = "Username must be between 3 and 25 characters.";
            }
            if (($repeatpass != $password) === true){
                $errors = "Passwords don't match!";
            }
            if (strlen($password) < 2 || strlen($password) > 15){
                $errors[] = "Password must be between 2 and 25 characters.";
            }
            if ((ctype_alnum($username) || ctype_alnum($password)) === false){
                $errors[] = "Password and username must use only letters and numbers.";
            }
            if ((filter_var($email, FILTER_VALIDATE_EMAIL)) === false){
                $errors = "Please enter a valid email address.";
            }
            
            if (empty($errors)){
                register($name, $username, $password, $email);
                echo "You have successfully registered!";
            }else{
                ?>
                <p>Failure in registering.</p>
                <ul>
                <?php
                foreach ($errors as $error){
                    echo "<li>$error</li>\n";
                }
                ?>
                </ul>
                <?php
            }
            
        }
?>
Error wrote:Warning: Invalid argument supplied for foreach() in /Users/jon/Sites/user/register.php on line 43
what's the problem?

Re: Registering

Posted: Tue Jul 05, 2011 10:02 pm
by Tino
On line 26 in the code you posted, you set the error variable to a string if no valid email address was supplied. You also do this on line 17. You should use that bracket notation you use on the other lines to make sure the errors get added to the $errors array.
$errors[] = 'string';

Re: Registering

Posted: Tue Jul 05, 2011 10:05 pm
by JelvinJS7
Tino wrote:On line 26 in the code you posted, you set the error variable to a string if no valid email address was supplied. You also do this on line 17. You should use that bracket notation you use on the other lines to make sure the errors get added to the $errors array.
$errors[] = 'string';
Oh I see. :oops: hehe. Thanks!
How did I miss that?…

Re: Registering

Posted: Tue Jul 05, 2011 10:30 pm
by Tino
I don't know ;)

But you're welcome, anyway.