Welcome to WebDesignForums.net!
You're currently viewing WDF as a guest. By registering for a free account, you'll be able to participate with other members in our friendly community. Being a member allows you to ask questions and get answers for those troublesome web development tasks!

In addition, as a member you'll be able to post your websites up for review. Using our unique website review system you can gain some amazing feedback from some of the best web developers around. This is a completely free service to all registered members.

Ready to register yet? Registration is 100% free. Click Here To Join Now!

PHP form validation

Discussion in 'PHP' started by adrian jones, May 29, 2012.

  1. Offline

    adrian jones Member

    Message Count:
    84
    Likes Received:
    1
    Trophy Points:
    8
    Gender:
    Male
    I am currently working on a registration form for my site.

    On submit, it runs through a script, which is as follows:
    PHP:
    <?php
     
    if ($_POST) {

    // Yes it has been Posted.....and should be full of data from the form
    // Need to Validate the data HERE
    //

        
    $firstname=$_POST['firstname'];
        
    $surname=$_POST['surname'];
        
    $gender=$_POST['gender'];
        
    $email=$_POST['email'];
        
    $contact=$_POST['contact'];
        
    $gmc=$_POST['gmc'];
        
    $quals=$_POST['quals'];
        
    $region=$_POST['region'];
        
    $system=$_POST['system'];
        
    $confirm=$_POST['confirm'];

    // Check if the Email has been used before
        
    $query "SELECT * FROM Locums where email = '$email'";
        
    $result mysql_query($query);

    // If Error with the Select.....
        
    if (!$result) {
            echo 
    "Could not access Table";
            die( 
    print_rmysql_error(),true));
        }

    //Select was OK - did it return anything?
        
    $rows mysql_num_rows$result );
        if (
    $rows === true) {
          die(
    "You have already registered with that e-mail address.");

    // *****should Redisplay the page with error.....
    }
     
    // Nothing returned so OK to create this record in the Locums Table

    // Generates random activation code
        
    $activ_code=uniqid(''true);
        
    $query="INSERT INTO Locums(firstname, surname, gender, email, contact, gmc, quals, region, system, confirm, uniquecode) VALUES ('$firstname','$surname','$gender','$email','$contact','$gmc','$quals','$region','$system','$confirm','$activ_code')";
        
    $result=mysql_query($query);

        if (!
    $result) {
            echo 
    "Error Inserting Record";
            die( 
    print_rmysql_errors(),true));
        }

    ?>

    All the data inserts in to the table correctly with a unique activation code etc etc but on testing, the email validation does not seem to be working because it allows me to duplicate an email address.

    Can anyone help with this?

    Thanks.


  2. Online

    mlseim WDF Staff

    Message Count:
    5,484
    Likes Received:
    224
    Trophy Points:
    63
    Gender:
    Male
    Location:
    Cottage Grove, Minnesota
    PHP:

     
    // Check if the Email has been used before
        
    $query "SELECT * FROM Locums where email = '$email'";
    //die here if there is a problem.
        
    $result mysql_query($query) or die(mysql_error());

    // No need to do this ... if a row is found,  that means it already exists.
    // If Error with the Select.....
    //    if (!$result) {
    //        echo "Could not access Table";
    //        die( print_r( mysql_error(),true));
    //    }

    //Select was OK - did it return anything?
        
    $rows mysql_num_rows$result );
    // if a row was found, the email has already been used.
        
    if ($rows 0) {
          die(
    "You have already registered with that e-mail address.");

    // *****should Redisplay the page with error.....
    }



  3. Offline

    adrian jones Member

    Message Count:
    84
    Likes Received:
    1
    Trophy Points:
    8
    Gender:
    Male
    Excellent, here he is... Mr mlseim, the man I can always count on to solve my woes.

    How are you? thanks for the reply by the way, works a treat.
    One more thing you may be able to help me with now.

    The error appear on the page, but I would like the error to appear just above the form so that the user doesn't have to go back, they can just amend their errors and re-submit.

    I did find this bit from a previous code but if you could help me implement this, it would be much appreciated.

    PHP:
    <?php    
         
    if(!empty($err))  {
           echo 
    "<div class=\"msg\">";
          foreach (
    $err as $e) {
            echo 
    "* $e <br>";
            }
          echo 
    "</div>";    
           }
    ?>


  4. Online

    mlseim WDF Staff

    Message Count:
    5,484
    Likes Received:
    224
    Trophy Points:
    63
    Gender:
    Male
    Location:
    Cottage Grove, Minnesota
    That will be tougher because when you submitted the form,
    you execute another script. The refresh erases your form.

    You'll have to either use AJAX (PHP and javascripting), or somehow
    save all of your form variables so you can redisplay the form with
    the boxes still filled-in.

    I think you should go with the all AJAX form w/validation.

    Like these examples ...
    http://www.noupe.com/php/beautiful-forms.html

    Or some of these various techniques:
    http://www.noupe.com/css/47-excellent-ajax-css-forms.html

    You can still check if an email is already used.
    But if they type an email and click to the next box, it will tell them
    right away if it's used or not ... even before they submit.

    I guess there are so many ... you need to Google them to find the features you need.


  5. Offline

    adrian jones Member

    Message Count:
    84
    Likes Received:
    1
    Trophy Points:
    8
    Gender:
    Male
    OK, I may just leave this as is and inform the user that they have to click the back button to rectify their errors.
    Last thing I wanted to look in to is to highlight the input at the time of them completing the form if they leave it empty, on a previous google search for something else I stumbled across this to put in the input:
    PHP:
    <?php if($_POST[comments] == ""){ echo "class=\"error\""?>
    and then just have .error textarea {color: #} in the css, will this work?


  6. Offline

    adrian jones Member

    Message Count:
    84
    Likes Received:
    1
    Trophy Points:
    8
    Gender:
    Male
    Obviously with comments changed to whatever the input name is.


  7. Online

    mlseim WDF Staff

    Message Count:
    5,484
    Likes Received:
    224
    Trophy Points:
    63
    Gender:
    Male
    Location:
    Cottage Grove, Minnesota
    Repost the entire thing.
    I can't grasp how the page will refresh after validation.


  8. Offline

    adrian jones Member

    Message Count:
    84
    Likes Received:
    1
    Trophy Points:
    8
    Gender:
    Male
    I think this will do for now, It checks for duplicate e-mail addresses and wont submit if there is already one there, and if not it will process the form, it has hints but they will still submit blank if the user leaves them out, hopefully they will see the * and realise they must be completed.

    http://tinyurl.com/7qh263p


  9. Offline

    adrian jones Member

    Message Count:
    84
    Likes Received:
    1
    Trophy Points:
    8
    Gender:
    Male
    Just a few more queries regarding this form.
    On displaying the form, it looks absolutely fine (see link in previous post) and when submitted correctly, the thank you message is also fine, but if a duplicate email address is entered and the error message displays, it cannot be read because my white background does not show and it is as if the script has just stopped loading, there is no footer, only header and black text with the error.

    It is this bit that generates the error:
    PHP:
    //Select was OK - did it return anything?
        
    $rows mysql_num_rows$result );
    // if a row was found, the email has already been used.
        
    if ($rows 0) {
          die(
    "You have already registered with that e-mail address. Please click back on your browser to go back and try again.");

    // *****should Redisplay the page with error.....
    }


  10. Online

    mlseim WDF Staff

    Message Count:
    5,484
    Likes Received:
    224
    Trophy Points:
    63
    Gender:
    Male
    Location:
    Cottage Grove, Minnesota
    Repost the entire thing ... something else must be causing your error.

    And turn on error reporting so you can see what the error really is.
    If you're not sure what I mean ... Google it: php error reporting


  11. Offline

    adrian jones Member

    Message Count:
    84
    Likes Received:
    1
    Trophy Points:
    8
    Gender:
    Male
  12. Offline

    adrian jones Member

    Message Count:
    84
    Likes Received:
    1
    Trophy Points:
    8
    Gender:
    Male
    Just wondering if its the die before the error message that is stopping the script, considering thats what die does lol

    i've changed die to echo and this displays the error with the rest of the page displaying as I want it, except the other bit of text saying thanks for registering displays underneath... I thought that was only supposed to display if the page was loaded NOT from a post, so i'm taking a guess in saying I do need the die somewhere, but the echo can stay where the error is?


  13. Offline

    adrian jones Member

    Message Count:
    84
    Likes Received:
    1
    Trophy Points:
    8
    Gender:
    Male
    I'm still having problems with this form.
    I changed the die to a redirect script so that if the email address is already in the database it has this:
    PHP:
        $rows mysql_num_rows$result );
    // if a row was found, the email has already been used.
        
    if ($rows 0) {
          
    header('Location: http://www.siteurl.net/error');
    // *****should Redisplay the page with error.....
    }
    However this doesnt work, all I get is this message:
    Warning: Cannot modify header information - headers already sent by (output started at /home/adejones/public_html/locumregister.php:5) in /home/adejones/public_html/locumregister.php on line 105
    and the script seems to carry on because after this is has the rest of the "thank you for registering" bla bla, any ideas? anyone?

    Thanks


  14. Online

    mlseim WDF Staff

    Message Count:
    5,484
    Likes Received:
    224
    Trophy Points:
    63
    Gender:
    Male
    Location:
    Cottage Grove, Minnesota
    sorry ... just haven't had the time to look at this.

    it's summer here (in Minnesota) ... a few months out of the year when it's warm enough to venture out in shorts.


Share This Page