2013-02-26 244 views
0

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

我的第一页代码如下─

<?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(); 

?> 
+1

你的代码在哪里? – 2013-02-26 04:27:05

回答

3

你正在寻找一个叫做模式Post/Redirect/Get

实质上,在表单发布之后,执行你所有的MySQL信息和然后使用以下重定向

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

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

+1

不要忘记'位置:'。即,'header('Location:thanks.php')'。 – adamdunson 2013-02-26 04:29:49

+0

我没有得到它..请解释更多。 – 2013-02-26 04:45:56

+0

应在表格完成处理后完成'header' – 2013-02-26 04:48:07

0

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

if(isset($ _ POST [“submit”])){ $ date = $ _ POST [“date”];

然后在INSERT QUERY后使用HEADER。 如此

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

+0

您能否请编辑我的代码正确执行此操作.. – 2013-04-06 17:29:14