避免刷新页面上的数据提交php ['PHP_SELF']
问题描述:
我在表单中使用<?php echo $_SERVER['PHP_SELF']; ?>
。 当我刷新页面时提交表单,它一次又一次地提交表单。避免刷新页面上的数据提交php ['PHP_SELF']
请帮助我避免在刷新页面上重新提交表单。
这里是PHP代码:
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;
?>
我不想使用头。我在同一页上打印成功消息
答
请在提交表单后不要显示回复。只需将其重定向到任何其他页面。
header('Location: /path/to/record');
exit;
+0
我不想使用标题,我正在使用echo在同一页上打印成功消息 –
+0
如果您不想使用标题,那么您可以先处理数据后取消设置$ _POST变量。例如,未设置($ _ POST);然后在下一步中使用头部('Location:'。$ _ SERVER ['PHP_SELF'])将页面重定向到同一页面中。 – Jigsdatta
向我们显示此页的代码。 – Tajgeer
可能的重复[最好的方式来避免由于刷新页面提交](https://*.com/questions/5690541/best-way-to-avoid-the-submit-due-toa-a-页面刷新) – Ali786
它是浏览器的预期行为(在本例中发布请求时重新创建了以前的请求),以便在处理发布请求后更改您需要重定向的行为。 – Filip