Skip to main content
GameDev.net gamedev.net
🔒 Locked

[web] Making Refresh not re-post form data?

Started by BeanDog Aug 10, 2006 at 1:49 PM 6 replies 1.5k views
Original Post
BeanDog
BeanDog
When a user inputs information into a form and submits it, my PHP code processes the information and redirects them via a header('location:...') to a landing page. On the landing page, when a user clicks Refresh, it asks them if they would like ot re-post the form data. Is there a way to prevent this option from being displayed? I guess that instead of doing a header() call I could send a style redirect, but is there a better (generally-accepted) way of doing this?
walle
walle
When you have processed the data, send them to the page again without the form being filled.

<php// Use the data$name = $_REQUEST['name'];// Open the page again, but now you havn't posted any data so no message box will bee seenheader('Location index.php');?>



or whatever page you display...
Mathachew
Mathachew
I don't think walle's example will work. When you redirect, any regular variable will be lost. Only global/registered variables can be used.

Also, something doesn't sound right because if you are redirecting, then reposting should be broken. Maybe if you're posting to a specific page, but I'm really not too certain.

Here's how I normally break reposting, because God knows I hate having to confirm my refresh on form submissions and that I am skeptical of user input:

<form action="" method="post">	<insert form data>	<input type="submit" name="submit" value="Submit" /></form>


then in some file that will handle the posted data:
<?php	if(array_key_exists('submit', $_POST))	{		// yadda yadda		// can do error checking here		if($error)		{			$_SESSION['error'] = $error;			$_SESSION['post'] = $_POST;		}		header('Location: filename.php');	}	else	{		include 'some_file.html';	}?>


In filename.php
<?php	if(array_key_exists('error', $_SESSION))	{		$name = $_SESSION['post']['name'];		// etc. etc. for the different fields that you want to maintain the data		$error = "You're missing this, that, and that.";		unset($_SESSION['error']);		unset($_SESSION['post']);	}	include 'filename.html';?>


I hope this helps! :)
walle
walle
This do work, I do it all the time...
For an example, Someone registers on your page, name and password. Then you get the data from the form, stick it in a SQL database and reload the page, or in this case to the main page or somthing...
tstrimp
tstrimp
Walle's method works fine, it was just unclear on how to use it. Make sure you handle the $_POST data BEFORE you redirect, otherwise it will get thrown away. Then you redirect to a page without any post data. This removes the POST warning when refreshing and will prevent the data from being processed twice.
Mathachew
Mathachew
Quote:
Walle's method works fine, it was just unclear on how to use it. Make sure you handle the $_POST data BEFORE you redirect, otherwise it will get thrown away. Then you redirect to a page without any post data. This removes the POST warning when refreshing and will prevent the data from being processed twice.


This is precisely what I was implying. :)
markr
markr
There is no way to stop the browser re-posting the data. The trick is to avoid processing it again, which you can do by setting a session variable, cookie etc, to know that you've already done that.

Mark
Mathachew
Mathachew
Quote:
There is no way to stop the browser re-posting the data. The trick is to avoid processing it again, which you can do by setting a session variable, cookie etc, to know that you've already done that.


Sure there is, it's outlined above. You simply handle your posted data and redirect without printing anything out. This will most assuredly break re-posting in most, if not all, browsers (I say this because I don't know it will for all, but then again, it is server side, so it should).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.