Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP - How to detect postback and page refresh

Posted on 2009-05-18
16
Medium Priority
?
1,688 Views
Last Modified: 2012-05-07
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
Comment
Question by:lwfuk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 7
16 Comments
 
LVL 4

Expert Comment

by:trencH87
ID: 24414225
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
 
LVL 4

Expert Comment

by:trencH87
ID: 24414271
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
 

Author Comment

by:lwfuk
ID: 24414402
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 4

Expert Comment

by:trencH87
ID: 24414584
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
 

Author Comment

by:lwfuk
ID: 24414676
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
 
LVL 4

Expert Comment

by:trencH87
ID: 24414710
<?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
 
LVL 4

Expert Comment

by:trencH87
ID: 24414734
Btw, it wasnt identical. I renamed the input type name to "submit" - or else $_POST['submit'] wouldnt work.
0
 

Author Comment

by:lwfuk
ID: 24414742
The example above continually registers 'Form isnt submitted. Please submit the form.'
0
 

Author Comment

by:lwfuk
ID: 24414771
Take a look again.

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

Note: type="submit"
0
 
LVL 4

Expert Comment

by:trencH87
ID: 24414852
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
 

Author Comment

by:lwfuk
ID: 24414928
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
 
LVL 4

Expert Comment

by:trencH87
ID: 24415942
Thats what the header is for. Please read up on www.php.net/header
0
 

Author Comment

by:lwfuk
ID: 24416105
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
 
LVL 4

Expert Comment

by:trencH87
ID: 24418998
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
 
LVL 4

Accepted Solution

by:
trencH87 earned 500 total points
ID: 24419001
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
 

Author Closing Comment

by:lwfuk
ID: 31582672
Marvellous! Thats what I call a good solution.
Well Done.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question