Clear out form values after submitting form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JeffS
Forum Newbie
Posts: 12
Joined: Thu Mar 06, 2003 8:05 pm

Clear out form values after submitting form

Post by JeffS »

A user fills out a form on page 1. They are then sent to page 2 which displays the "posted" information. If everything is okay, page 2 runs a function which inserts the information into a database and the user is redirected to page 3 which lets them know that the data has been added to the database.

Now...if the user hits the browser 'Back' button and goes back to page 1, the information that was entered into that form is still there and, therefore, it is possible for the user to enter the same data again and again.

So...how can I stop this from happening? That is, once a user has submitted that information I do *not* want them to be able to submit it repeatedly.

Please note (in case this is relevant in this case) that I'm using Sessions to pass info from page to page.

Thanks in advance for your help,

Jeff
net7
Forum Commoner
Posts: 31
Joined: Wed Mar 12, 2003 1:27 pm

Post by net7 »

I admit I do not know how for sure. But I do have two ideas.

One.)

In each of the input tags you could include "value=''". For example :

Code: Select all

<input type="text" name="username" value="">
Two.)

Another way it could be done is in javascript. PHP does not handle things like this and it would probably require a client-side script. I am not familiar with too much javascript, but javascript can do many things through forms.
craigh
Forum Newbie
Posts: 19
Joined: Sun May 19, 2002 2:50 pm

Re: Clear out form values after submitting form

Post by craigh »

JeffS wrote:A user fills out a form on page 1. They are then sent to page 2 which displays the "posted" information. If everything is okay, page 2 runs a function which inserts the information into a database and the user is redirected to page 3 which lets them know that the data has been added to the database.
first of all, you need to structure it different:

page 1: form
page 2: display info
page 3: if OK, process data to DB, display success/failure

So, in order to keep a user from inserting multiple instances of the same data, on page 3, before you insert data, you lookup data that is the same and if so, do not do the insert. This is best acheived with somekind of unique identifier (hidden on the first page form). You can then look this up in the DB and if it exists, don't insert it again - OR - just update the current values...

:)
JeffS
Forum Newbie
Posts: 12
Joined: Thu Mar 06, 2003 8:05 pm

Re: Clear out form values after submitting form

Post by JeffS »

craigh wrote:
So, in order to keep a user from inserting multiple instances of the same data, on page 3, before you insert data, you lookup data that is the same and if so, do not do the insert. This is best acheived with somekind of unique identifier (hidden on the first page form). You can then look this up in the DB and if it exists, don't insert it again - OR - just update the current values...

:)
Hmmm....I'm gonna have to give that some thought. It's possible that I could use the person's email address as a way to determine if there is, indeed, a duplicate record (essentially...if there is already a record from the same person with the same email address and then if the other fields match...BINGO!).

Jeff
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

It also depends on which browser you use. Mozilla is nice and friendly and, if you use the BACK-button, remembers the values you entered into the database. IE doesn't (don't remember Opera and Konqueror).

On the first form, have your form-tags like this:

Code: Select all

<input type=text name=myText size=35 maxlength=50 value=<?=$sessionVariable&#1111;'myText']?>
On the second form, after you've inserted into the database,
session_unregister("sessionVariable").

Another way would be to have a javascript function run on form 1, which assigns every formfield an empty value.

Off the top of my head (code might need some correction) this will clear the values of all formfields of your first form on your page (if you have more than one form, you need another for-next loop).

Code: Select all

<script language="JavaScript" type="text/javascript">
for(i=0;i<document.form&#1111;0].length;i++)
   &#123;document.form&#1111;0].elements&#1111;i].value="";&#125;
</script>
JeffS
Forum Newbie
Posts: 12
Joined: Thu Mar 06, 2003 8:05 pm

No luck with Session Vars

Post by JeffS »

Thanks for your suggestion.
I tried the following:
value="<?php echo stripslashes(trim($_SESSION['srYd_sYardName']))?>"

and then, after submitting, added this:

session_unregister('srYd_sYardName');
session_destroy();

However, IE 6 still retained the values in the form.

The Javascript solution, though I didn't try it, would seem to be problematic since you want people to be able to go back to the form and make changes before they do the final submit.

Jeff
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

I personally favour the approach that craigh suggested, where you check certain fields in the table to determine whether identical data has been entered before. Since it's a server-side solution you don't have to worry about which browser the user is using and whether or not they've got JavaScript enabled.

As for destroying sessions, when you are using $_SESSION, you should not be using session_register(), session_is_registered() or session_unregister(). So instead of:

Code: Select all

session_unregister('srYd_sYardName'); 
session_destroy();
you would do

Code: Select all

unset($_SESSION);
session_destroy();
However, that won't have (as you discovered) any effect on what you get if you press the back button since the page is not reloaded before being displayed.

Mac
JeffS
Forum Newbie
Posts: 12
Joined: Thu Mar 06, 2003 8:05 pm

Thanks for suggestion & help

Post by JeffS »

Sorry for the late reply. I've been buried in other programming issues.

I also like CraigH's suggestion. That may be the one I try first.

Jeff
Post Reply