Clearing Form after submit

What is the normal behaviour of the form after submission. Is the data submitted still in the form? (in case post to itself). If not how would you clear the form using only PHP?

It depends on how the form is coded. In other words, you can have just plain form-controls such as text input boxes, textareas, etc. In that case, once the form is ‘submitted’, the information entered will disappear.

But often forms are coded so that the form-controls ‘retain state’, meaning that they retain the information entered even after the form submit button is hit. This is done as a convenience for the user so that if there is a validation error and the user is sent back to the form to fix it, he doesn’t have to complete the whole form all over again. He just has to make the correction.

Usually a user is redirected away from the form to a ‘success’ message on a different page if the form is submitted successfully. If they then go back to the form, it will no longer contain the information entered.

3 Likes

You want to learn about PRG. https://en.wikipedia.org/wiki/Post/Redirect/Get

Yeah i was hoping I could get away without taking a user to another page. Is there a way to use a sessions to prevent double submission?

Put your form processing code in a different file. Then redirect back to the page the form was on and display the message there conditional on a successful submission. Unless you code it otherwise, the information will no longer be in the form. And the form will not get submitted twice.

I have only had the problem with double submissions when I put the form processing code at the top of the page the form is on, and I don’t do that properly. I have always found it safer to use a separate php file for the processing of the information.

I know what you mean. My problem is following…

  • I have done all validation/error messages using Jquery and works great

  • I then thought of …wait what if user disabled JS?

  • Right now I am doing all server side validation and for example user submits “Stribor5” as a first name. I send this toserver for validation and it comes back with "not good only letters no digits allowed. I want to display again "“Stribor5” so user can just correct and delete “5”. This is all happening on the page where form is (post to itself).

  • If I put separate file I wouldn’t not be able to display value as per bullet above

You should definitely always do front-end AND server-side validation.

The front-end validation is for the user’s convenience, so they can correct issues as they go through the form.

The server-side validation is always done after the form is completee and submitted. In the form processing file, if the validation fails, the form data will not be processed, and the user is sent back to the form (pre-populated with his data from the $_POST global array - that’s what I meant by ‘retain state’) with error messages to make the corrections.

If the validation passes, the user is sent back to the form page with a success message and the form is no longer populated with the form data.

1 Like

Does this happen automatically?

How do you send error message with JS disabled to form page from php processing page. So for example you submit form page to process form page (php) and find out that user entered email wrong. How do you send back to form page with error message that email is wrong?

automatically?

When i’ve created forms i’ve always posted it to itself and used php to check the variables before inserting or if they don’t meet the criteria output the form again.

  • page loads no ‘submit’ variable set so output form
  • form filled → submitted to same page
  • submit variable set so check variable are as should be
    -if ok submit to database - hide form - output nice message
  • if not ok show form again and print errors out for user

If you echo the post varibles as values into the form they will show nothing when the user gets there and if the validation shows the form again the variables will be populated in the fields to be amended and resubmitted.

JS can still check before user submits forms.

hope that makes sense

You have to code all of this to happen. Nothing happens automatically.

The error messages don’t depend on JS, so it doesn’t matter whether JS is enabled or disabled. It doesn’t come into the picture at all with server-side validation.

I don’t have time now, but if you can wait until later in the day, I can post some sample code to illustrate what I mean (if someone else doesn’t come along earlier with some code). In the meantime you could Google PHP and server-side validation. There are a lot of good examples out there. Just be careful you don’t get one that is out of date or that contains errors.

Yes later in the day is totally fine

NO, NO and NO. If you implemented PRG you would have no problem. All the code should be in the same page.

Implement PRG and you will not have a double submission problem. Simple as that.

This is wrong. Do not depend on the name of a button to be submitted in order for your script to work. It will completely fail in certain circumstances. You need to check the REQUEST METHOD.


if($SERVER['REQUEST_METHOD'] == 'POST'){
// Process Form
}
1 Like

Can you show me snippet of code (pseudo code) how would this work. Everything right now is on same page

can you elaborate so i know where i am going wrong?

Start your own post with your single page code and I will take a look. We don’t want to hijack the OP’s thread.

Basic example


<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
        {
        //Validate data. Put errors in error array if any

        if (no_errors){
            //process form
            die(header("Location: ./sameform.php"));
        }
        else{
        display error messages
        repopulate form with submitted data
        }

?>
// Display Form

Is this PHP code at the top of the page?
When you see no errors and you use header to redirect doesn’t data submitted shows in the form since you redirecting to same page?

I can think of two ways.

Initially assign an $error_message variable to an empty string, always display that in the page, when the variable gets assigned a message value, the message will display.

Write a conditional that will display the message when there is an error.

1 Like