0

I have a webpage that allows users to submit a query and get result in email.The user selects values for three variables and the email address. My problem is that everytime I refresh the page the form resubmits itself with old values and send the email (i.e I am not even clicking on submit query). I tried using $_POST=array() but it is still not working.

Here is my code:

<?php
if(isset($_POST['submit'])){
  $varApp= $_POST['App'];
  $varConfig = $_POST['Config'];
  $varCtrType = $_POST['CtrType'];
  $varEmail = $_POST['mailid'];

exec("/py $varApp $varConfig $varCtrType 2>&1",$output );
if ($output[8] == "Empty"){
echo "<div style ='font:22px Arial,tahoma,sans-serif;color:#ff0000'><br>No Data Available! <br></div>";
}
else {
     exec(' printf "Please find attached the query result for following selection:\n\nApp: '.$varApp.'  \nConfig: '.$varConfig.' \nCounter Type: '.$varCtrType.' \n\n Thanks! "  | /bin/mail -s "Database Query Result" -a '.$output[8].' '.$varEmail.'  2>&1', $output2 ); 
echo "<div style ='font: 18px Arial,tahoma,sans-serif;color:#10ac84'><br><b> Please check your email for result !<b> <br>";
echo '<script language="javascript">';
echo 'alert("Please check your email for result! Submitted Query details: Selected App: '.$varAPP.' Configuration:")';
echo '</script>';
}
$_POST=array();

}
?>


</body>

I have not given the html part here. So, everytime a user refreshes the page he gets an email again with previous session query results. Any guidance here is highly appreciated. Note: I am not using mail or pHPmailer here but that is not what I need to discuss here.

Thanks,

Aisha
  • 91
  • 7

1 Answers1

0

Taken from this answer:

To prevent users from refreshing the page or pressing the back button and resubmitting the form I use the following neat little trick.

if (!isset($_SESSION)) {
    session_start();
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $_SESSION['postdata'] = $_POST;
    unset($_POST);
     header("Location: ".$_SERVER['PHP_SELF']);
    exit;
}  
?>

The POST data is now in a session and users can refresh however much they want. It will no longer have effect on your code.

Jamie_D
  • 979
  • 6
  • 13