Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Nov 2009
    Location
    Florida United States
    Posts
    106
    Thanks
    6
    Thanked 2 Times in 2 Posts

    server side validation if else syntax help

    Im still a beginner with php and using a book to learn. I'm sure its some kind of syntax error but any help would be great. I am trying to do basic server side validation and when the variable $output_form is true I want it to display the form. The form isn't showing when I first load the page but I used

    if(isset($_POST['submit'] to try and correct this. Please help

    Code:
     if(isset($_POST['submit'])) {
        
        $first_name = $_POST['first_name'];
        $last_name = $_POST['last_name'];
        $user_name = $_POST['user_name'];
        $password = $_POST['password'];
        $repassword = $_POST['repassword'];
        $email = $_POST['email'];  
        $output_form = false;  
          
        if(!empty($first_name) && !empty($last_name) && !empty($user_name) && !empty($password) && !empty($repassword) && !empty($email)){
          
        }else $output_form = true; //end if
        
        if($output_form == true){
      ?> 
      
      <form method = "post"
           action = "<?php echo $_SERVER['PHP_SELF']; ?>"
           name = "sign_up_form"
           onSubmit = "return validate_signup ();">
          <label for = "first_name">First Name:</label>
          <input type = "text"
                 name = "first_name"
                 id = "first_name" /> <br />
          <label for = "last_name">Last Name:</label>
          <input type = "text"
                 name = "last_name"
                 id = "last_name" /> <br />   
          <label for = "user_name">User Name:</label>
          <input type = "text"
                 name = "user_name"
                 id = "user_name" /> <br /> 
          <label for = "password">Password:</label>
          <input type = "text"
                 name = "password"
                 id = "password" /> <br />
          <label for = "repassword">Retype Password:</label>
          <input type = "text"
                 name = "repassword"
                 id = "repassword" /> <br />
          <label for = "email">E-Mail:</label>
          <input type = "text"
                 name = "email"
                 id = "email" /> <br />
          <button type = "submit" 
                  name = "submit">Submit</button>
      </form>
      <?php
      }}
      ?>

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Validate and sanitise your input.

    Code:
    <?php
    
    if(isset($_POST['form_sent']))
    {
    
        $first_name = $_POST['first_name'];
        $last_name = $_POST['last_name'];
        $user_name = $_POST['user_name'];
        $password = $_POST['password'];
        $repassword = $_POST['repassword'];
        $email = $_POST['email'];
        $output_form = false;
    
            if(!empty($first_name) && !empty($last_name) && !empty($user_name) && !empty($password) && !empty($repassword) && !empty($email))
            {
                    [processing code here]
            }
    }
    else
    {
    ?>
      <form method = "post"
           action = "<?php echo $_SERVER['PHP_SELF']; ?>"
           name = "sign_up_form"
           onSubmit = "return validate_signup ();">
          <input type="hidden" name="form_sent"/>
          <label for = "first_name">First Name:</label>
          <input type = "text"
                 name = "first_name"
                 id = "first_name" /> <br />
          <label for = "last_name">Last Name:</label>
          <input type = "text"
                 name = "last_name"
                 id = "last_name" /> <br />
          <label for = "user_name">User Name:</label>
          <input type = "text"
                 name = "user_name"
                 id = "user_name" /> <br />
          <label for = "password">Password:</label>
          <input type = "text"
                 name = "password"
                 id = "password" /> <br />
          <label for = "repassword">Retype Password:</label>
          <input type = "text"
                 name = "repassword"
                 id = "repassword" /> <br />
          <label for = "email">E-Mail:</label>
          <input type = "text"
                 name = "email"
                 id = "email" /> <br />
          <button type = "submit"
                  name = "submit">Submit</button>
      </form>
    <?php
    }
    ?>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •