-1

I have 3 php pages, on the first I have a form where the user can enter his account (email adress) and the amount he wants to deduct from his account. On the second I have the call of the function and the sucess message shown. On the third page the function send() is done, which open the DB connection, change the amount for the account and save it.

First page:

<form action="send_exec.php" method="post">
Amount: <input type="number" size="24" maxlength="4"
name="points_send" min="0"><br><br>
Account<input type="text" size="24" maxlength="50"
name="email"><br><br>
<input type="submit" value="Send">
</form>

Second page (send_exec.php):

<?php
include("update_send.php");
$email = $_POST["email"];
$points_send = $_POST["points_send"];
settype ($points_send, "integer"); 
send($email, $points_send);
echo ("Success");
?> 

Third page (update_send.php):

//some calculations are done before this to get $new_points
function send($email, $points_send)
{
$mysql_update_points = "UPDATE user SET points = ('$new_ponints') WHERE email = ('$email')";
$mysql_update = mysql_query($mysql_update_points);
}

Problem is now that when I perform the action on the first page and I end up on the second: How can I disable that when just clicking F5 / reload in my browser that the action is called again?

davejagoda
  • 2,420
  • 1
  • 20
  • 27
Schwallo
  • 3
  • 1
  • 5
    Same way everyone else does: move the browser to a different page. – Ignacio Vazquez-Abrams Nov 25 '15 at 10:33
  • The easiest way is to redirect the browser to a different page on success; move `echo("Success");` to a new page/script (let's name it `success.php`) and replace it with `header('Location: /success.php');` in `send_exec.php`. – axiac Nov 25 '15 at 10:37
  • see e.g. https://en.wikipedia.org/wiki/Post/Redirect/Get – VolkerK Nov 25 '15 at 11:01
  • 1
    As nobody has mentioned this i'll do it. Please do not use [mysql_](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) functions as they are deprecated and in PHP 7.0 deleted. Please use [mysqli_](http://php.net/manual/en/book.mysqli.php) functions or use [PDO](http://php.net/manual/en/book.pdo.php) – BRoebie Nov 25 '15 at 11:07
  • Thank you, I wasn't aware of this! – Schwallo Nov 25 '15 at 12:09
  • @Schwallo no problem glad to help – BRoebie Nov 25 '15 at 12:55

2 Answers2

0

Make a fourth page called "done.php".

Code:

<?php
$pageMsg = isset($_GET['msg']) ? $_GET['msg'] : '';

if($pageMsg === "success"){
      echo "Success";
   } else {
      echo "Error";
   };
?>

Then on the third page change your source to this:

//some calculations are done before this to get $new_points
function send($email, $points_send)
{
$mysql_update_points = "UPDATE user SET points = ('$new_ponints') WHERE email = ('$email')";
$mysql_update = mysql_query($mysql_update_points);

if($mysql_update === FALSE) { 
    //die(mysql_error()); //Remove comment if you want to get the error.
    header("Location: done.php?msg=error");
} else {
    header("Location: done.php?msg=success");
    };
}

Lastely change your second page(send_exec.php) from:

<?php
include("update_send.php");
$email = $_POST["email"];
$points_send = $_POST["points_send"];
settype ($points_send, "integer"); 
send($email, $points_send);
echo ("Success");
?> 

To this:

<?php
require_once("update_send.php");
$email = $_POST["email"];
$points_send = $_POST["points_send"];
settype ($points_send, "integer"); 
send($email, $points_send);
?> 
Emil Elkjær
  • 685
  • 1
  • 9
  • 31
  • Don't put the redirect into the `send()` function. Make it return a boolean (the result of the database operation) and let the calling code decide what to do next (redirect to a different page). This way the function can be used again in a different context, where the continuation can be different. – axiac Nov 25 '15 at 11:11
  • Thank you very much! This is helping me so much!! – Schwallo Nov 25 '15 at 12:09
  • @emil-elkjær-nielsen Is there a way to return also a value here: `else { header("Location: done.php?msg=success"); };` Like return($new_points); But how I do I get the value to the page done.php? – Schwallo Nov 25 '15 at 13:28
  • I would probably create a function somewhat like this: function getPoints($account){ //do stuff here return $YourVarNameHere; } On your third page, then call that function from done.php – Emil Elkjær Nov 25 '15 at 13:59
  • Thank you, I will try it out. – Schwallo Nov 25 '15 at 14:08
-2

I think you need to check whether the form is submitted or not.

You can check this by using the code: if ($_SERVER["REQUEST_METHOD"] == "POST") { ... }

Please check form validations before proceeding...

Prime_Coder
  • 161
  • 2
  • 14
  • That's not how a page refresh works. When the user asks the browser to refresh the page, the previous request is re-sent as-is (using the same method and parameters as before). – axiac Nov 25 '15 at 10:42
  • well in that case, we can use header('location:filename.php'), which will take back to the original page with form. right? – Prime_Coder Nov 25 '15 at 10:49
  • This situation is usually handled by redirecting the browser (using `header('Location: ...');` indeed) to another page after the form submit is successfully processed. This new page displays the outcome of the submit processing. It can learn how the processing went either from a GET parameter or from a cookie (the session). Displaying the form page makes sense when the submitted data doesn't validate and also when the application logic allows submitting the form again with different data. – axiac Nov 25 '15 at 11:07