Jump to content

Separating Form Display from Processing


doubledee

Recommended Posts

Can someone give me some guidance of how to separate Form Display from Form Processing?

 

I have always used forms that submitted back to themselves which isn't so bad, but then trying to cram in code to display the form, validation errors, and messages after the form is processed all in one file is insane?!

 

Currently I am working on a simple "Add a Comment" form.

 

It would be nice to have a separate form processing script, but I don't know where to begin...

 

 

Debbie

 

Link to comment
Share on other sites

I place my js validating section in the head of the page that holds the form just to make sure errors and omissions are caught before the form is processed.

 

In the <form action="PUT FORM PROCESSING FILE NAME HERE" ...  Do the same thing in this file as you were did in the one page form. Get your form variables and validate and  process. Then go to next web page.

Link to comment
Share on other sites

I typically have one page, but that page will include() the code necessary to either validate and/or display the form

 

Rough example on the "main" page (e.g. 'myform.php')

//Var to hold validation error (if form was posted)
$errors = array();

if(isset($_POST['someformfield']))
{
    //Include the file that does the form validation
    include('form_processing_page.php');

    //If any validation errors occur, they will be added to the $errors array

    //If validation passes the validation page can process/save the data OR call another page to do that logic
    //But, at the end of processing, user is redirected to a confirmation page using header() followed by exit()
    //so the remaining code on this page is not executed
}

//Include the html form page
// This only happens if the form was not submitted or if there were validation errors
include('form_html.php');

//The html content page will include logic to display the errors if the exist

Link to comment
Share on other sites

I typically have one page, but that page will include() the code necessary to either validate and/or display the form

 

Rough example on the "main" page (e.g. 'myform.php')

//Var to hold validation error (if form was posted)
$errors = array();

if(isset($_POST['someformfield']))
{
    //Include the file that does the form validation
    include('form_processing_page.php');

    //If any validation errors occur, they will be added to the $errors array

    //If validation passes the validation page can process/save the data OR call another page to do that logic
    //But, at the end of processing, user is redirected to a confirmation page using header() followed by exit()
    //so the remaining code on this page is not executed
}

//Include the html form page
// This only happens if the form was not submitted or if there were validation errors
include('form_html.php');

//The html content page will include logic to display the errors if the exist

 

That is basically what I do now, except for another script printing out the results.

 

My big problem - and I don't see how your approach is immune - is if the user hits Back/Forward/Back/Forward then you get into Double-Submission issues...  :-\

 

 

Debbie

 

Link to comment
Share on other sites

All patterns have strengths and weaknesses.  That's why they're patterns and not silver bullets.  Each pattern is a common, tested, working approach to solve a particular problem.  Your problem is double submissions.  The PRG Pattern exists to solve that particular problem.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.