1

I am using <?php echo $_SERVER['PHP_SELF']; ?> in form. After submit form when I refresh page it submit form again and again.

Kindly help me avoid resubmit of form on refresh page.

here is PHP code:

if (isset($_POST["patientName"])){
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "INSERT INTO `inpatient`(`patientId`, `patientName`, `guardianName`, 
    `age`, `gender`, `mobile`, `address`, `city`, `symptoms`, `reference`, `date`, `time`, `doctorName`, `email`)
    VALUES('$patientId', '$patientName', '$guardianName', '$age', '$gender', '$mobile', '$address', '$city', '$symptoms', 
    '$reference', '$date', '$time', '$doctorName', '$email')";


    $conn->exec($sql);
}
    echo '
        <div style="position:absolute; bottom:0; left:0; z-index:1000; " class="alert alert-icon-left alert-success alert-dismissible fade in mb-2" role="alert">
                                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                        <span aria-hidden="true">x</span>
                                    </button>
                                    <strong>Well done!</strong> You successfully entered the <a href="#" class="alert-link">Patient</a> data.
                                </div>
    ';
}
    catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }

$conn = null;



 ?>

I don't want to use header. I'm printing success message on same page

aadil asad
  • 13
  • 4
  • Show us code of this page. – Tajgeer Jul 17 '17 at 10:49
  • 2
    Possible duplicate of [Best way to avoid the submit due to a refresh of the page](https://stackoverflow.com/questions/5690541/best-way-to-avoid-the-submit-due-to-a-refresh-of-the-page) – smali Jul 17 '17 at 10:50
  • It's intended behavior of browser (recreated previous request in this case post requests) to change that you need to redirect after handling post request. – cymruu Jul 17 '17 at 10:51
  • @Tajgeer I've updated the code. Please check. – aadil asad Jul 17 '17 at 10:58

1 Answers1

0

Please do not show the response after the form is submission. Just redirect it to any other page.

header('Location: /path/to/record');
exit;
Jigsdatta
  • 11
  • 2
  • i don't want to use header, I'm using echo to print success message on same page – aadil asad Jul 17 '17 at 10:54
  • If you don't want to use header then you can first unset the $_POST variable after you process your data. For ex, unset($_POST); and then in the next step redirect your page in the same page using header('Location:'.$_SERVER['PHP_SELF']); – Jigsdatta Jul 17 '17 at 11:02