Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form and removing $_POST Variables

Status
Not open for further replies.

axman505

Technical User
Joined
Jun 20, 2001
Messages
489
Location
US
Hello,

I have a page that has a form on the top. WHen you hit submit, the actino is set to the same page and the entered information is then displayed down below. My problem is, that if you refresh the page after filling out the form, the information is sumbited again and the form is double filled. Is there anyway I can tell it to only do it once and not multiple times?

Thanks,
 
yes, several... figure out what works best for you.

Perhaps use a session variable, once the form is submitted you fill it in, then you no longer process the form if resubmitted.

Or, if they should be able to change the information and resubmit, hash up some type of key from the information and store that either in a session variable or a database or whatnot, and don't reprocess if the information is a duplicate.

Process the form and then use the header directive to get away from the page, or back to it with an empty $_POST variable. Then you have the back button to worry about however.

 
Yes
try something like
Code:
if (!isset($_GET["bla"]))
{
 do your thing
}
else
 Dont do your thing

JR
As a wise man once said: To build the house you need the stone.
Back to the Basics!
 
obviously if I would have paid more attention I would have replaced $_GET with $_POST

JR
As a wise man once said: To build the house you need the stone.
Back to the Basics!
 
That doesn't help on a resubmit unfortunately, as if it was set on the original submit, it will still be set on the resubmit.
 
You could store the original POSTed values in an array (serialized and stored in a SESSION variable). When the form is resubmitted, compare the new values with those stored and if they are all the same, do nothing.

Ken
 
Kenrbnsn is correct.
I have done this before.
However it can be done easier

As far as I know it reads the form variables (name=bla) as $bla and you can check on them.

I have a script doing this at home. Will have a look how I fixed it.

But reading into an array may save you a lot of work in checking vars

JR
As a wise man once said: To build the house you need the stone.
Back to the Basics!
 
Ok, what exactly happens after the form on submit?
Cause maybe you can look at if you wish to execute that.
The way that it may be possible is to include a hidden check field that changes value the moment the submit button is pressed.

For my own form solution I indeed use the if(!$HTTP_POST_VARS) rule and it hides my form after these values have been submitted.

But without knowing exactly what happens on your page it is hard to comment.

JR
As a wise man once said: To build the house you need the stone.
Back to the Basics!
 
Ok, you are correct. refresh is a bit of a pain! I am trying to find a solution now. Hopefully one of the experts can help?

JR
As a wise man once said: To build the house you need the stone.
Back to the Basics!
 
Having to deal with refreshing pages is a pain. Especially in applications that are step bound. That's where session based control comes in.
I wrote an application that requires steps to be followed in a particular sequence. To get around the refreshing business - as any client side code is not 100% fool proof (including meta tags, javascript, cache directives) I started keeping the actual step number in the session and sucessfully prevent stepping back. The script is all at the same URL and calls the appropriate "visible" page by the session variable.
For the current problem:
Set a toggle that shows that the page was submitted. Once the toggle is set to "on" replace the form with the information but offer a link that allows to correct that information and start over.
If the toggle is already on and the user uses the back button and re-submits then you can decide how to handle it. It's mainly a policy question. What do you do? Many options:
1. Overwrite the info siltently
2. Ignore the new info silently
3. Prompt the user to choose
4. Overwrite but notify
5. Ignore but notify
etc.
You need to decide which is the path you want.
Also, from the generality of the question it is hard to advise you what to do. It depends on the function of the page within your application.
 
I've used session variables and hashes on form data:

Code:
<?php
/*  The form which submits to this script is:

<html>
	<body>
		<form method="post" action="test_sha_form.php">
			<input type="hidden" name="form_name" value="some_form1">
			<input type="text" name="foo"><br>
			<input type="text" name="bar"><br>
			<input type="submit">
		</form>
	</body>
</html>

*/
session_start();

$sha = sha1(serialize($_POST));

print '<pre>';

print_r ($_SESSION);
print_r($_POST);
print "\n" . $sha . "\n";

$process_data = FALSE;
if (!isset($_SESSION['form_data']))
{
	$_SESSION['form_data'] = array();
	$_SESSION['form_data'][$_POST['form_name']] = $sha;
	$process_data = TRUE;
}
else
{
	if (!isset($_SESSION['form_data'][$_POST['form_name']]))
	{
		$_SESSION['form_data'][$_POST['form_name']] = $sha;
		$process_data = TRUE;
	}
	else
	{
		if ($sha != $_SESSION['form_data'][$_POST['form_name']])
		{
			$_SESSION['form_data'][$_POST['form_name']] = $sha;
			$process_data = TRUE;
		}
	}
}

if ($process_data == TRUE)
{
	print 'processing data';
}
else
{
	print 'not processing data';
}

?>



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
ok, I found a way to fix this problem:

Code:
0. start a session
1. Check for your if(!$HTTP_POST_VARS)
2. if this is not available add a session var $_session["check"] = "true"
3. Display the form.

4. For the code to be executed after the form was displayed do ELSEIF($_SESSION['check'] == "true") (first you checked with if(!$HTTP_POST_VARS) and after submitting the page this is no longer true so it will go to the else).

5. Unset ($_SESSION['check']);
6. Do your thing.
7. Refresh and refresh and refresh and nothing will happen!

Worked for me!
How does this work?
First time you execute the $_SESSION['check'] = "true" statement because nothing was posted.

Then something was posted so you go to the second statement.
This will execute because the session variable is set.

Then refresh. First statement (form creation) is skipped because it was already submitted.

Second statement is skipped because the session variable does no longer exist!

JR
As a wise man once said: To build the house you need the stone.
Back to the Basics!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top