0

我使用 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();

?>
4

2 回答 2

3

您正在寻找一种称为Post/Redirect/Get的模式

本质上,在表单发布后,完成所有 MySQL 信息,然后使用以下重定向:

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

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

于 2013-02-26T04:27:45.757 回答
0

您需要在 PHP 中将此提交按钮用作 POST。尝试这个!!!!

if(isset($_POST["submit"])){ $date=$_POST["date"];

然后在 INSERT QUERY 之后使用 HEADER。像这样

echo header("location:thank.php");

于 2013-02-26T05:56:32.210 回答