Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1759
  • Last Modified:

PHP - How to detect postback and page refresh

I have a php page that posts back to itself. Each time somebody clicks the submit button they get charged a credit. I don't want the customer to pay if they refresh the screen - only if they hit the submit button. How can I distinguish between postback and refresh?

0
lwfuk
Asked:
lwfuk
  • 9
  • 7
1 Solution
 
trencH87Commented:
You should at first auto-refresh the site after submit to be sure the end-user don't get charged more credits than he want.

Assuming you have a submit button named "submit", this is the way to check a post.
<?
if (isset($_POST['submit'])) {
    // Charge credit
}
?>

Open in new window

0
 
trencH87Commented:
Btw, to auto-refresh the site after submit, use this:
<?
if (isset($_POST['submit'])) {
    // Charge credit code here
 
    // Refresh
    header("location: " . $_SERVER['REQUEST_URI']);
}
?>

Open in new window

0
 
lwfukAuthor Commented:
Hi trencH87

Could you test this and tell me what is wrong. It always resisters 'Refresh'

Kind Regards,

Adrian Smith
London


<?php
 
if (isset($_POST['submit'])) {
    echo 'Post';
	header("location: " . $_SERVER['REQUEST_URI']);
} else {
	echo 'Refresh';
}
 
?>
 
<html>
<body>
<form id="f" name="f" method="post" action="<?php $PHP_SELF; ?>">
  <input type="submit" name="button" id="button" value="Submit" />
</form>
</body>
</html>

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
trencH87Commented:
Yup
<?php
 
if (isset($_POST['submit'])) 
{
    
    // Type in the code for charging credits here.
 
    // header() in this case refreshes the site.
    header("location: " . $_SERVER['REQUEST_URI']);
 
} else 
{
    echo 'Form isnt submitted. Please submit the form.';
}
 
?>
 
 
<html>
<body>
<form id="test" name="test" method="post" action="">
  <input type="submit" name="submit" id="button" value="Submit" />
</form>
</body>
</html>

Open in new window

0
 
lwfukAuthor Commented:
trencH87

It doesn't work. Did you test it?

The example you posted is identical to mine except that you have missed off:

action="<?php $PHP_SELF; ?>"
0
 
trencH87Commented:
<?php $PHP_SELF; ?> is really unnessasary in this case. And yes, I tested it. Remember that it shouldnt print out anything when submitting the form. The code I gave you just refreshes the site when the form is submitted. Its up to you to implement the function for charging credits.
0
 
trencH87Commented:
Btw, it wasnt identical. I renamed the input type name to "submit" - or else $_POST['submit'] wouldnt work.
0
 
lwfukAuthor Commented:
The example above continually registers 'Form isnt submitted. Please submit the form.'
0
 
lwfukAuthor Commented:
Take a look again.

ie:  <input type="submit" name="button" id="button" value="Submit" />

Note: type="submit"
0
 
trencH87Commented:
Just copy&paste this code, and you'll see that it works.

Note: name="submit". the "type" isnt read by $_POST[] - the "name" is.
<?php
 
if (isset($_POST['submit'])) 
{
	// Insert the code for charging credits here.
	
	// header() in this case refreshes the site. 
	//Uncomment this when you want to use it.
    #header("location: " . $_SERVER['REQUEST_URI']);
 
	echo "Form is submitted!";
 
} 
 
?>
 
 
<html>
<body>
<form id="test" name="test" method="post" action="">
  <input type="submit" name="submit" id="button" value="Submit" />
</form>
</body>
</html>

Open in new window

0
 
lwfukAuthor Commented:
Unfortunatly it still doesn't work.  It prints "Form is submitted!" when you post. It also prints it when you refresh. Test the code below. The counter continually counts whether you refresh or post.


<?php
 
session_start();
 
if (isset($_POST['submit'])) 
{
        // Insert the code for charging credits here.
        
        // header() in this case refreshes the site. 
        //Uncomment this when you want to use it.
    #header("location: " . $_SERVER['REQUEST_URI']);
 
        echo "Form is submitted!";
		
		$_SESSION['count']++;
		
		echo $_SESSION['count'];
		
		
 
} 
 
?>
 
 
<html>
<body>
<form id="test" name="test" method="post" action="">
  <input type="submit" name="submit" id="button" value="Submit" />
</form>
</body>
</html>

Open in new window

0
 
trencH87Commented:
Thats what the header is for. Please read up on www.php.net/header
0
 
lwfukAuthor Commented:
Sorry trencH87 but we are just going round in circles.

Do you have a working example? If so, could you post it or if not sombody else reply.

Regards,

Adrian Smith

0
 
trencH87Commented:
Try this one then.
<?php
session_start();
 
if (isset($_POST['submit'])) 
{
	// Insert the code for charging credits here.
	$_SESSION['count']++;
	
	// header() in this case refreshes the site. 
	//Uncomment this when you want to use it.
	#header("location: " . $_SERVER['REQUEST_URI']);         
 
}
 
echo "Charged points: " . $_SESSION['count'];
 
?>
 
 
<html>
<body>
<p>
<form id="test" name="test" method="post" action="">
  <input type="submit" name="submit" id="button" value="Submit" />
</form>
</p>
</body>
</html>

Open in new window

0
 
trencH87Commented:
Im sorry, this one:
<?php
session_start();
 
if (isset($_POST['submit'])) 
{
	// Insert the code for charging credits here.
	$_SESSION['count']++;
	
	// header() in this case refreshes the site. 
	//Uncomment this when you want to use it.
	header("location: " . $_SERVER['REQUEST_URI']);         
 
}
 
echo "Charged points: " . $_SESSION['count'];
 
?>
 
 
<html>
<body>
<p>
<form id="test" name="test" method="post" action="">
  <input type="submit" name="submit" id="button" value="Submit" />
</form>
</p>
</body>
</html>

Open in new window

0
 
lwfukAuthor Commented:
Marvellous! Thats what I call a good solution.
Well Done.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 9
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now