表单重新提交的PHP重定向标头不起作用
我尝试了一些在这个网站上找到的关于防止表单重新提交的方法。但是,由于某些原因,重定向函数不会触发。 我能知道我做错了什么吗?我怎样才能纠正它表单重新提交的PHP重定向标头不起作用,php,html,forms,Php,Html,Forms,我尝试了一些在这个网站上找到的关于防止表单重新提交的方法。但是,由于某些原因,重定向函数不会触发。 我能知道我做错了什么吗?我怎样才能纠正它 <?php if(isset($_POST['bookingsubmit'])) { $error = '0'; require_once("makebooking.php"); $i_username = $_SESSION['username']; $i_slotno = isset($_POST['slotinp
<?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>
预订
预约时间
姓名:
输入插槽号:
车辆编号:
车辆类型:货车
拖车
所需空间:
在开始呈现HTML
之前,您应该添加header()
,因为您的代码似乎没有在HTML
中间使用它的意义
<?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);
}
if(isset($_POST['bookingsubmit']))
{
header("Location: " . $_SERVER['REQUEST_URI']);
}
?>
<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>
预订
预约时间
姓名:
输入插槽号:
车辆编号:
车辆类型:货车
拖车
所需空间:
您已将HTML内容放在标题之前。在返回任何输出之前应使用标题。我假设您正在尝试实现一种模式。对于重定向部分,必须在将任何输出发送到用户代理之前发送。要避免缓存,请使用响应代码
<?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
}
?>
预订
预约时间
姓名:
输入插槽号:
车辆编号:
车辆类型:货车
拖车
所需空间:
除非使用输出缓冲,否则一旦开始发送HTML内容,就不能简单地回显header()
。在HTML输出之前,使用或移动您的header()
调用来包装整个内容。谢谢。在我处理完数据后将其移动到。谢谢谢谢我正在寻找一个这样的例子。