Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

PHP form validation

  • 30-06-2009 03:42PM
    #1
    Registered Users, Registered Users 2 Posts: 197 ✭✭


    I have a php form which is used to create a database entry. There is a separate validation file which checks for errors before updating the database. If an error occurs then it redirects back to the data entry page and an error message is displayed.
    I want to retain the values for all the fields that were entered when the page is redisplayed. How would I do this? I was thinking of an object variable which is registered as a session variable in the validation file and then used in the data entry page. Is there any other way to do this?

    Thanks

    Dave


Comments

  • Registered Users, Registered Users 2 Posts: 6,651 ✭✭✭daymobrew


    Could you merge the form into the validation script so that the submitted variables are available when you echo the form?

    [PHP]// Pseudo code
    $ValidData = 0;
    if ( $_POST ) {
    // Validate each field.
    // When all fields valid then $ValidData = 1;
    // This code would come from your current validation script.
    }

    if ( $ValidData ) {
    // Write data to database
    }
    else {
    // Display form setting values as $_POST etc
    }
    [/PHP]


  • Registered Users, Registered Users 2 Posts: 8,070 ✭✭✭Placebo


    You should use this for validation, its better if the page doesnt change and it shows errors before submission [http://bassistance.de/jquery-plugins/jquery-plugin-validation/]

    loads more out there


    other wise, use POST obviously, then GET the variables on the error page, then POST them back [invisible form fields]?


  • Registered Users, Registered Users 2 Posts: 197 ✭✭cracker


    Thanks for the suggestions

    I was trying to separate the business logic from the presentation which is why I have the code in two separate files. I think this is good practice.

    The JQuery looks good but I am not sure it is what I am looking for. I am already doing some basic javascript validation on the page for mandatory fields, date formats etc. But the validation I am talking about involves business rules and I need to access the database to do it. So I think PHP is the way to go for this type of validation.

    If I am redirecting back to the data entry page using header("Location: .....) is it possible to use POST. I thought in this sceario I can only use GET by including them in the url?


  • Registered Users, Registered Users 2 Posts: 8,070 ✭✭✭Placebo


    well if they are going back to entry page using Location then surely just insert the GET'd values in the url 'http://domain.com/back.php?p=whatever' and have GET on the main page as well.

    depends what way you want to send them back, via a href link just use location.
    i think location is more robust way.


  • Registered Users, Registered Users 2 Posts: 6,651 ✭✭✭daymobrew


    cracker wrote: »
    I was trying to separate the business logic from the presentation which is why I have the code in two separate files. I think this is good practice.
    Maybe, on error, you could read in the presentation page (file_get_contents or similar) and put in the values and then echo it out. This will keep the stuff separate.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,045 ✭✭✭Bluefrog


    The way I handle this usually is:

    I have a script with my form and calls for processing, lets call it form.php. It's broken up into two states, pre-post and post and in the post state I include my validation routines so I'd have 2 main functions, doForm and processForm, called like this:
    if(!$_POST)
    {
    doForm();
    }
    else
    {
    processForm();
    }
    
    function doForm($response = NULL)
    {
    //here I output my form interface when rendering the interface I check to see which state my form is being rendered in, pre-post or post and if it's in post I populate each form field with the appropriate value from the $_POST vars.
    }
    
    function processForm()
    {
    $response = array();
    include('validation.php');
    //here I run my validation methods
    //if a field fails, the error message gets added to the $response array
    if there are no errors, $response is empty and I continue to process the data
    if $response contains errors I call the doForm function passing in $response.
    so something like
    if(count($response) == 0)
    {
    //code to process data
    }
    else
    {
    doForm($response);//The important thing is that the $_POST vars are still available to me here so I can populate the fields as they were submitted
    }
    }
    

    Keeps it nice and clean and I don't have to do any unnecessary redirection.


  • Registered Users, Registered Users 2 Posts: 197 ✭✭cracker


    Nice one. That seems like a good way to do it. Will give it a whirl

    Cheers

    Dave


Advertisement