Jump to content
  • Advertisement
Sign in to follow this  
BeanDog

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

This topic is 4450 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 <meta http-equiv> style redirect, but is there a better (generally-accepted) way of doing this?

Share this post


Link to post
Share on other sites
Advertisement
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 seen
header('Location index.php');
?>




or whatever page you display...

Share this post


Link to post
Share on other sites
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! :)

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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. :)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!