Results 1 to 4 of 4

Thread: php form... redisplay if validation fails

  1. #1
    Join Date
    Sep 2008
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default php form... redisplay if validation fails

    This is probably really easy for most people who know php, but I don't... so here's my problem:

    I have a php form with a reCaptcha. The form works fine, but if the recaptcha validation fails, it clears up the page and displays an error message, forcing the user to use the back button if they want to try again.
    this is what my code currently looks like:

    if (!$resp->is_valid) {
    die ("The validation code entered was incorrect. Please try again." .
    );


    what I'd like to do, is be able to display the error in a pop-up window, keeping the filled out form displayed so that the user can try again (as many times as their heart desires).

    what should I put instead of the "die" ? how do I make that happen?

  2. #2
    Join Date
    Oct 2008
    Posts
    42
    Thanks
    0
    Thanked 9 Times in 9 Posts

    Default

    In order to use a pop-up window, you'd have to be doing your validation in javascript. Using js for validation is OK, but you would still have to perform the validation server-side in your php code, since javascript can easily be disabled.

    An alternative would be to continue to do the validation in your php code, and if validation fails, redirect the user back to the input page with the input variables filled in with the user's input and an error message.

    To redirect, you'd use:
    PHP Code:
    if (!$resp->is_valid) {
         
    header("Location: url_of_input_form)"
    ); 
    replacing url_of_input_form with the url of your input form.

  3. The Following User Says Thank You to BabblingIdjit For This Useful Post:

    aegraphic (10-03-2008)

  4. #3
    Join Date
    Sep 2008
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    o.k. So
    header("Location: url_of_input_form)"

    redirects my user back to the form. will it automatically display his data, or do I need to add something more?
    and where would the error message be displayed in this case?

  5. #4
    Join Date
    Oct 2008
    Posts
    42
    Thanks
    0
    Thanked 9 Times in 9 Posts

    Default

    No, it will not automatically display his data. You'll have to provide the logic to insert the user's input into the form fields. I would typically display the error message above or below the form fields.

    This means you'll have to include your logic above the display code. So, in pseudo-code, your file would look something like:

    Code:
    <?php
    if (form_submitted)
    {
        get input variables;
        validate_input();
    
        if (input_valid)
        {
             process input variables;
             redirect to next page;
        }
        else
        {
            populate output variables with user input;
            set flag to display error message;
        }
    }
    else
    {
        populate output variable as null;
    }
    
    display form;
    ?>

Bookmarks

Posting Permissions

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