a conditional statement to enable form action.

Discussion in 'PHP' started by Cazman, Jul 25, 2009.

  1. #1
    I was wondering if anyone knew of a way to redirect to a page only if the conditions in the script are met. Here's the whole page:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org
    /TR/xhtml1-strict.dtd">
    <html>
    <head>
    <link rel="stylesheet" href = "homestyle.css" />
    <title>Register on our database</title>
    </head>
    <body>
    <div class = "formclass"> 
    <form method = "post" name = "form1" action = "registered.php">
    <label>Username: </label><input class="user" type = "text" name = "user" /><br /><br />
    <label>Password: </label><input class="pass" type = "password" name = "pass" /><br /><br />
    <label>Verify password: </label><input class="pass2" type = "password" name = "pass2" /><br /><br />
    <label>E-Mail: </label><input class="email" type = "text" name = "email" />
    <input type = "submit" value = "register" />
    </form>
    </div>
    <?php
    
    $user = $_POST['user'];
    $pass = $_POST['pass'];
    $pass2 = $_POST['pass2'];
    $email = $_POST['email'];
    
    if ($user == "")
    {
    echo "<br /><h5>Please enter a username</h5>";
    }
    
    elseif ($pass == "")
    {
    echo "<br /><h5>Please enter a password</h5>";
    }
    
    elseif ($pass2 == "")
    {
    echo "<br /><h5>Please make sure both password fields are filled in</h5>";
    }
    
    elseif ($pass != $pass2)
    {
    echo "<br /><h5>please make sure both passwords are the same</h5>";
    }
    
    elseif ($email == "")
    {
    echo "<br /><h5>Please enter an E-Mail address</h5>";
    }
    
    ?>
    </body>
    </html>
    Code (markup):
    So if all the conditions are true, then the page runs the form action.
     
    Last edited: Jul 25, 2009
    Cazman, Jul 25, 2009 IP
  2. zeronese

    zeronese Peon

    Messages:
    83
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    if i understand your question right, i think you already have the code :)
    just add an else statement at the end of your code..

    so you would have
    if ($user == "")
    {
    echo "<br /><h5>Please enter a username</h5>";
    }
    
    else if ($pass == "")
    {
    echo "<br /><h5>Please enter a password</h5>";
    }
    
    else if ($pass2 == "")
    {
    echo "<br /><h5>Please make sure both password fields are filled in</h5>";
    }
    
    else if ($pass != $pass2)
    {
    echo "<br /><h5>please make sure both passwords are the same</h5>";
    }
    
    else if ($email == "")
    {
    echo "<br /><h5>Please enter an E-Mail address</h5>";
    }
    else
    //do the action now
    PHP:
     
    zeronese, Jul 25, 2009 IP
  3. Cazman

    Cazman Peon

    Messages:
    43
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The code is there, but instead of running through all of the conditions, it just skips straight to registered php, I want it to only go to registered.php if all the conditions return true. So say if I haven't entered any info at all, it goes straight to registered, even with the else statement. I'm also not familiar with the php syntax that enables form action.
     
    Last edited: Jul 26, 2009
    Cazman, Jul 26, 2009 IP
  4. Amator

    Amator Well-Known Member

    Messages:
    1,424
    Likes Received:
    55
    Best Answers:
    0
    Trophy Points:
    165
    #4
    with php you can only check after submitting the form
    you need to do the check in registerd.php (in your case)

    or if you use javascript you can check before submitting the form.
     
    Amator, Jul 26, 2009 IP
  5. Cazman

    Cazman Peon

    Messages:
    43
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks Amator, I've looked into it, and it seems the only solution is to create a javascript. Thanks everyone, and I'll update on this thread if it works. (It's just practice, so it isn't a big deal. ut I might want to use this in the future. And one more note, when I run the script, on the actual page I get this error:

    Notice: Undefined index: user in C:\Program Files\wamp\www\index.php on line 20

    And it's the same with each line where I declare each input as a variable.
     
    Last edited: Jul 26, 2009
    Cazman, Jul 26, 2009 IP
  6. zeronese

    zeronese Peon

    Messages:
    83
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6

    in this case you'll have to put your form on a seperate page and do a redirection to the registered.php when all your conditions are satisfied.
     
    zeronese, Jul 26, 2009 IP