如何在刷新页面时停止重新提交页面?

时间:2013-02-26 04:25:26

标签: php html mysql forms

我使用php作为服务器端脚本并使用表单在数据库中使用MySql插入数据。提交页面重定向到另一个页面并显示感谢等。但是当用户单击刷新按钮然后表单再次提交时,所以请告诉我如何阻止这一点。

我的第一页代码如下 -

<?php

$route=$_POST['route'];
$datee=$_POST['SelectedDate'];
$pick_time=$_POST['pick_time'];
$ampm=$_POST['ampm'];
$destination=$_POST['destination'];
$cab=$_POST['cab'];
$days="/";


?>

<html>
<head>



</head>
<body>
<form onSubmit="validateForm();" action="http://localhost/wordpress/?page_id=452"             method="post" name="localhire_select" >
<table>
<tr>
<td>
<div id="outer" name="outer" align="left" style="width:300px;height:300px;background-        color:#FAFA23;border:1px solid;padding:10px 30px;">
<table width="200" border="0" cellpadding="0" cellspacing="2">
<tbody>
<tr>
<td  widtn="60%" align="left">Pick-up Date:</td><td><input type="text" name="datee"     size="10px"  value=<?php echo $datee ; ?> /></td>
</tr>
<tr>
<td width="60%" align="left">Pick-up Time :</td><td><input type="text" name="time" size="10px"  value=<?php echo $pick_time."".$ampm; ?> /></td>
</tr>
<tr>
<td width="60%" align="left">Destination :</td><td><input type="text" name="destination" size="10px"  value=<?php echo $destination; ?> /></td>
</tr>
<tr>
<td width="60%" align="left">No. of Days :</td><td><input type="text" name="days" size="10px"  value=<?php echo $days; ?> /></td>
</tr>
<tr>
<td width="60%" align="left">Type of Cab :</td><td><input type="text" name="cab" size="10px"  value=<?php echo $cab; ?> /></td>
</tr>


</tbody>
</table>
<table width="200" border="0" cellpadding="0" cellspacing="2">
<tbody>
<tr>
<td height="71" valign="middle" align="center"><input name="changeDetails" type="image" id="button" value="Submit" src="change.jpg"></td></td>
</tr>
</tbody>
</table>
</div>

</td>
<td>
<div id="right" name="right" align="right" style="width:440px;height:300px;background-color:#F7EA2A;border:1px solid;padding:10px 30px;">

<table>
<tr style="background-color:orange;border:1px solid black;" height="30">
<td><b>Gurgaon-><?php echo $destination; ?> ->Gurgaon </b></td><td>
</td>
</tr>
<tr  height="20">
<td><input type="hidden" name="route" value="<?php echo $route; ?>"  />
</td>
</tr>
<tr>
<td>Your Full Name :</td><td><input type="text" name="full_name" value="" size="20px"     /></td>
</tr>
<tr>
<td>Address :</td><td><input type="textarea" name="address" value="" size="20px 20px"     /></td>
</tr>
<tr>
<td>Mobile no. :</td><td><input type="text" name="mobile_number" value="" size="20px" /></td>
</tr>
<tr>
<td>Pick-up Location :</td><td><input type="text" name="pickup_location" value="" size="20px" /></td>
</tr>

<tr>
<table>
<tr align="center">
<td height="71" align="center" valign="middle">
            <input name="localhire_select" type="image" id="button" value="Submit" src="book_cab.jpg"></td>


</tr>
</table>           

 </tr>

</table>
</div>
</td>
</tr>
</table></form>
</body>
<html>

`

第二页的代码是 -

<?php
error_reporting(E_ALL);
ini_set('display_errors', true);

$host="localhost";
$user_name="usr";
$pwd="pwd";
$database_name="db"; //assuming you created this
mysql_connect($host, $user_name, $pwd);
if (mysql_error()>"") print mysql_error() . "<br>";
mysql_select_db($database_name);
if (mysql_error() > "") print mysql_error() . "<br>";

$datee=$_POST['datee'];
$time=$_POST['time'];
$destination=$_POST['destination'];
$days=$_POST['days'];
$cab=$_POST['cab'];
$route=$_POST['route'];
$full_name=$_POST['full_name'];
$address=$_POST['address'];
$mobile_number=$_POST['mobile_number'];
$pickup_location=$_POST['pickup_location'];

 $query2="SELECT `order_number` FROM `count` WHERE 1";
 $count=mysql_query($query2);
while($row = mysql_fetch_array($count))
{
   $variable = $row["order_number"];
}


$query3="UPDATE `count` SET order_number=order_number+1 WHERE 1";
mysql_query($query3);


$query1="INSERT INTO `booking`         (`pickup_date`,`pickup_time`,`destination`,`days`,`type`,`full_name`,`address`,`mobile`,`pi    ckup_location`,`route`,`order_number`) VALUES     ('$datee','$time','$destination','$days','$cab','$full_name','$address','$mobile_number','$    pickup_location','$route','$variable')";
mysql_query($query1);

mysql_close();

?>

2 个答案:

答案 0 :(得分:3)

您正在寻找名为Post/Redirect/Get

的模式

基本上,在发布表单后,使用以下内容执行所有MySQL信息,然后重定向

header('Location: thanks.php', true 303);

使用GET请求。然后,用户无法刷新该页面;它只会显示“谢谢”。

答案 1 :(得分:0)

你需要在PHP中使用此SUBMIT按钮作为POST。 试试这个!!!!

如果(isset($ _ POST [ “提交”])){ $日期= $ _ POST [ “日期”];

然后在INSERT QUERY之后使用HEADER。 喜欢这个

echo header(“location:thank.php”);