Jump to content

Press back button but don't resend data


mjsulliv

Recommended Posts

A form should not resend just because a user hits the back button. Do you mean you don't want a user to be able to submit the form again, after returning to the document through the back button?You could tie a javascript function to the forms submit event, and the function would disable the form somehow.A more complicated thing might be for PHP to generate a form input (type="hidden") whose value is the current timestamp (to ensure a unique value). Then, when a form is submitted, stash that value in a file or database. You also check that value against the stored values. If there is a match, do not process the form.

Link to comment
Share on other sites

I’m trying to find an elegant way, if there is one, to keep a form from wanting to resend its data when it is returned to by use of the back button. Any ideas are appreciated.
Do you mean when you return to a page which has received a form's submission? For example,Search page submits to link list page.Clicking a link on the link list page takes you to the link's page.Hit back button and it wants to resubmit the form (Meaning the search page's form, so it can generate the list page)Is that correct?If that is the case, I don't think you can stop that. If anything I would think it would be browser functionality, which you can't mess with. I'm not sure though.
Link to comment
Share on other sites

Hi. Situation is this: I have a form w/ two buttons -- a Submit button (Insert data into DB) and a button that loads a different PHP page (Display DB data). The form calls itself when its Submit button is clicked. When the form page is loaded - either initially or by calling itself -- PHP checks if this submit button has been clicked and if so performs a Insert into a DB. I can perform multiple submits w/ no problem; as well as multiple display DB button clicks. However if the user does a sequence of: Submit -- which then performs the Insert and redisplays the form -- and then clicks the Display DB button and then clicks Back, I get a message saying that in order to display the page (the form) the browser must resend information that will redo the last operation (the insert); and indeed it does evidenced by a message from the DB complaining of a duplicate key. Any ideas?

Link to comment
Share on other sites

Hi. Situation is this: I have a form w/ two buttons -- a Submit button (Insert data into DB) and a button that loads a different PHP page (Display DB data). The form calls itself when its Submit button is clicked. When the form page is loaded - either initially or by calling itself -- PHP checks if this submit button has been clicked and if so performs a Insert into a DB. I can perform multiple submits w/ no problem; as well as multiple display DB button clicks. However if the user does a sequence of: Submit -- which then performs the Insert and redisplays the form -- and then clicks the Display DB button and then clicks Back, I get a message saying that in order to display the page (the form) the browser must resend information that will redo the last operation (the insert); and indeed it does evidenced by a message from the DB complaining of a duplicate key. Any ideas?
That's what I thought was happening. In this case the page receiving the submitted form is the same page that contains the form. I don't think there is a way to stop that. What you could do instead is query the database before you do the insert to check if the record has already been inserted. If it already exists, display some kind of message to the user.
Link to comment
Share on other sites

After you process the form, use a location header to redirect the user. Even if you redirect them to the same page, the redirect won't include any of the form data so they can refresh that page all they want.

Link to comment
Share on other sites

After you process the form, use a location header to redirect the user. Even if you redirect them to the same page, the redirect won't include any of the form data so they can refresh that page all they want.
Can you elaborate a bit. Thanks
Link to comment
Share on other sites

<?php	//process form	header("location:{$_SERVER['PHP_SELF']}");	exit();?>

Link to comment
Share on other sites

<?php	//process form	header("location:{$_SERVER['PHP_SELF']}");	exit();?>

I guess I'm not sure where this code should go. Currently the php-file/page already calls itself
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" ....

Link to comment
Share on other sites

Put the header and exit line after you are finished processing the form.
I’ve placed this code in what I think is “the end of the form’s processing” but I get a “Cannot modify header information - headers already sent …” error. I begining to wonder if the issue maybe that I’m using a convoluted set of php files that are a mixture of HTML and PHP. The form’s action is to call itself:
<form action="<?php $_SERVER['PHP_SELF']; ?>"

Upon entry and re-entry, prior to this form statement, the $_POST array is tested for the submit button value and, if found, a php function (from another file) is called that does the work of the insertion into the DB and the insertion works fine.

Link to comment
Share on other sites

You might consider submitting the form to that other file and redirecting within that file instead. Just make sure that there isn't any HTML output of any kind before you try to send the redirect. That means any whitespace or HTML before <?php tags and any echo or print statements.

Link to comment
Share on other sites

I’ve decided to re-architect the application a bit. When the “Display DB data” button is clicked, instead of replacing the current window the DB data will be displayed in a second browser window – this eliminates the “back” issue calling the re-execution of the processing of the from. It also gives the users a better indication of what has been put in the DB, as all they have to do is refresh this new browser. The refresh of the form page after a “submit” still causes a re-execution and an error but this is not a big deal, the data is not inserted a second time, it’s just ugly. At some point, after I better understand the http protocol and the header() function I might revisit this code.Thank you all for help. I always learn something when I come here.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...