1

Below is my code, and is working fine.. The only problem is that when I refresh the page , It execute the success query.

<form action="" method="post">
<input type="submit" value="Purchase" id="btn" name="link_credits">
</form>

if(isset($_REQUEST['link_credits'])) 
{
echo "success";
//some codes here with sql statements
}
Else 
{ 
echo " Please try Again tomorrow"; 
}

?>

Problem:

I don't want to run the query with page refresh, it should run the code only at button click.

zeeshan
  • 35
  • 6

5 Answers5

1

Just add the line unset($_REQUEST['link_credits']); in success loop

if(isset($_REQUEST['link_credits'])) 
    {
    echo "success";
    //some codes here with sql statements

    if($success){
    $_SESSION['msg']="Data Added successfully";
    }
    else{
    $_SESSION['msg']="Data Not Added successfully.Please Check";
    }
    unset($_REQUEST['link_credits']);
    header("Location:--somepage.php--");    
    }
    else 
    { 
    echo " Please try Again tomorrow"; 
    }

Then you can show the $_SESSION['msg'] value in --somepage.php-- and after showing use unset($_SESSION['msg']);

Vikas Umrao
  • 2,800
  • 1
  • 15
  • 23
  • this works... but the problem is it does not display success message. can i set timer before it redirects to next page – zeeshan May 14 '15 at 13:58
  • To show the error or success message you need to use Session and then unset the session in next page; – Vikas Umrao May 14 '15 at 14:00
  • works well. except message not being displayed. i am redireting it to the same page, after uset... and trying to display echo "" after header(), but it does not work.. rest is ok.. – zeeshan May 14 '15 at 14:22
1

Your browser should be warning you that a page refresh will re-submit the form. So, you are clicking a button even when you refresh the page, or you are using a broken browser.

All that being said, here are some possible solutions:

  1. Cookie the browser during the "success" phrase, and only allow the success condition to run if the cookie is not set/present.

  2. Learn about "Redirect after POST", which is what you should be doing here. Essentially, you perform the success action, set flags as apropos, and forward the browser (via PHP's header()) to a page that says, "OK, it worked!".

Kevin_Kinsey
  • 2,285
  • 1
  • 22
  • 23
0

Change your if condition to.

if(isset($_POST['link_credits'])){
echo "success";
//some codes here with sql statements
}
else 
{ 
echo " Please try Again tomorrow"; 
}
Tittu Vaghese
  • 233
  • 3
  • 13
  • it run please try again even, I press the the button.. There are more than 5 forms with different names. i hope this should not impact the code – zeeshan May 14 '15 at 13:44
  • You can use any number of buttons, but each button should have unique names and use if conditions accordingly `if(isset($_POST['button_name']))` – Tittu Vaghese May 14 '15 at 13:47
0

This is because you are submitting the form to the same page with the method post, so when you refresh the page it novamento sent another request post.

Solution:

if(isset($_REQUEST['link_credits'])) {
  echo "success";
  $_REQUEST['link_credits'] = null;
} else {
  echo " Please try Again tomorrow"; 
}
0

This happen because of simply on refresh it will submit your request again.

So the idea to solve this issue by cure its root of cause.

I mean we can set up one session variable inside the form and check it when update.

if($_SESSION["csrf_token"] == $_POST['csrf_token'] )
{
// submit data  
}

//inside from 

$_SESSION["csrf_token"] = md5(rand(0,10000000)).time(); 

<input type="hidden" name="csrf_token" value=" 
htmlspecialchars($_SESSION["csrf_token"]);"> 
Amul
  • 149
  • 1
  • 3