Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP form validation

Options
  • 30-06-2009 3:42pm
    #1
    Registered Users 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 Posts: 6,509 ✭✭✭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 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 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 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 Posts: 6,509 ✭✭✭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 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 Posts: 197 ✭✭cracker


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

    Cheers

    Dave


Advertisement