gpt4 book ai didi

用于表单重新提交的 PHP 重定向 header 不起作用

转载 作者:搜寻专家 更新时间:2023-10-31 21:04:59 25 4
gpt4 key购买 nike

我已经尝试了在这个网站上找到的一些关于防止表单重新提交的方法。但是,重定向功能由于某种原因没有触发。我可以知道我做错了什么以及如何解决吗?

<?php
if(isset($_POST['bookingsubmit']))
{
$error = '0';
require_once("makebooking.php");
$i_username = $_SESSION['username'];
$i_slotno = isset($_POST['slotinput']) ? $_POST['slotinput'] : '';
$i_vehicleno = isset($_POST['vehiclenoinput']) ? $_POST['vehiclenoinput'] : '';
$i_vehicletype = isset($_POST['vehtype']) ? $_POST['vehtype'] : '';
$i_spacesreq = isset($_POST['spaceinput']) ? $_POST['spaceinput'] : '';
$error = makebooking($i_username,$i_slotno,$i_vehicleno,$i_vehicletype,$i_spacesreq);
}
?>

<HTML XMLns="http://www.w3.org/1999/xHTML">
<head>
<title>Make a booking</title>
</head>
<body>
<div id="makebookingsection">
<form id="makebookingform" method="post" action="">
<h4>Book a time</h4>
<p>Name : <?php echo $_SESSION['username']?></p>
<p>Enter Slot Number : <input type="number" name="slotinput"></p>
<p>Vehicle No. : <input type="text" name="vehiclenoinput"></p>
<p>Vehicle Type : <input type="radio" name="vehtype" value="Truck">Truck
<input type="radio" name="vehtype" value="Trailer">Trailer</p>
<p>Spaces Required : <input type="number" name="spaceinput"></p>
<input name="bookingsubmit" type="submit" value="Book" />
<?php
if(isset($_POST['bookingsubmit']))
{
header("Location: " . $_SERVER['REQUEST_URI']);
}
?>
</form>
</div>

最佳答案

我假设您正在尝试实现一个 Post/Redirect/Get (PRG) 模式。对于 Redirect 部分,您必须发送 header在任何输出被发送到用户代理之前。为避免缓存,请使用 303 响应代码。

<?php 
if(isset($_POST['bookingsubmit']))
{
$error = '0';
require_once("makebooking.php");
$i_username = $_SESSION['username'];
$i_slotno = isset($_POST['slotinput']) ? $_POST['slotinput'] : '';
$i_vehicleno = isset($_POST['vehiclenoinput']) ? $_POST['vehiclenoinput'] : '';
$i_vehicletype = isset($_POST['vehtype']) ? $_POST['vehtype'] : '';
$i_spacesreq = isset($_POST['spaceinput']) ? $_POST['spaceinput'] : '';
$error = makebooking($i_username,$i_slotno,$i_vehicleno,$i_vehicletype,$i_spacesreq);
header("Location: " . $_SERVER['REQUEST_URI'], true, 303);
exit;
}
else {
?>

<HTML XMLns="http://www.w3.org/1999/xHTML">
<head>
<title>Make a booking</title>
</head>
<body>
<div id="makebookingsection">
<form id="makebookingform" method="post" action="">
<h4>Book a time</h4>
<p>Name : <?php echo $_SESSION['username']?></p>
<p>Enter Slot Number : <input type="number" name="slotinput"></p>
<p>Vehicle No. : <input type="text" name="vehiclenoinput"></p>
<p>Vehicle Type : <input type="radio" name="vehtype" value="Truck">Truck
<input type="radio" name="vehtype" value="Trailer">Trailer</p>
<p>Spaces Required : <input type="number" name="spaceinput"></p>
<input name="bookingsubmit" type="submit" value="Book" />
</form>
</div>

<?php
}
?>

关于用于表单重新提交的 PHP 重定向 header 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34126628/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com