PHP表单 - 在提交后返回原始页面
我有一个非常简单的html页面,它调用一个php脚本将数据从表单提交到数据库。一旦用户提交了他们的数据,如果他们能够收到一条消息,说他们已经成功添加,然后将它们重定向到带有表单的页面,那就太好了。我使用的代码如下: HTML文件PHP表单 - 在提交后返回原始页面
<html>
<body>
<form action="insertcustomer.php" method="post" >
<p>First Name: <input type="text" name="firstname">
Last Name: <input type="text" name="lastname">
<P>Phone: <input type="text" name="phone"> Email: <input type="text" name="email"></P>
Address1: <input type="text"name="address1"> Address2: <input type="text" name="address2"> City:<input type="text" name="city">
<select name="Gender" id="Gender">
<option value="">Select One</option>
<option value = "1">Female</option>
<option value = "2">Male</option>
</select> <input value="Add" type="submit"> </form> </body>
</html>
PHP文件:
<?php
$con=mysqli_connect("centos-db.local","customerinfo","password","customerinfo");
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO `customers` (`firstname`, `lastname`, `email`,
`phone`, `address1`, `address2`, `city`, `gender`) VALUES
('$_POST[firstname]','$_POST[lastname]',
'$_POST[email]','$_POST[phone]', '$_POST[address1]' ,'$_POST[address2]'
,'$_POST[city]' , '$_POST[gender]')"; if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "Customer Added"; mysqli_close($con);
?>
你可以给任何帮助将不胜感激!
干杯
您既可以使用JavaScript:document.location.href="myFile.php";
或者你可以在你原来的形式张贴到同一页的形式在和处理消息出现。这也将有助于显示错误消息,如果有的话。
您可以使用其他操作。
<form action="/" method="post" >
设置帖子路径/检查是否设置了帖子变量,如果是这样,请给出消息。
这里的一个常见问题是,如果您重新加载页面,发布数据将再次发送。这就是您通常使用Post-Redirect-Get的原因。你可以在yoour php脚本的末尾使用类似的东西。
header("Location: {$_SERVER['HTTP_REFERER']}");
exit;
注意:如果你从另一个脚本重定向到显示一条消息,唯一的可能是发送PARAM在URL或将其保存到数据库,并要求originial页面上的最新消息。
重定向只需添加到您的主页,如:
<?php
header('Location: http://www.example.com/');
exit;
?>
你也应该考虑逃出用户输入,为安全起见。
试试这个它会工作:
可以显示在同一page.It的successfully submit message
会降低页面加载时间,提高用户体验。
的index.php:
<?php
if(isset($_POST['submit']))
{
$con=mysqli_connect("centos-db.local","customerinfo","password","customerinfo");
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO `customers` (`firstname`, `lastname`, `email`,
`phone`, `address1`, `address2`, `city`, `gender`) VALUES
('$_POST[firstname]','$_POST[lastname]',
'$_POST[email]','$_POST[phone]', '$_POST[address1]' ,'$_POST[address2]'
,'$_POST[city]' , '$_POST[gender]')"; if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "Customer Added"; mysqli_close($con);
}
?>
<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" >
<p>First Name: <input type="text" name="firstname">
Last Name: <input type="text" name="lastname">
<P>Phone: <input type="text" name="phone"> Email: <input type="text" name="email"></P>
Address1: <input type="text"name="address1"> Address2: <input type="text" name="address2"> City:<input type="text" name="city">
<select name="Gender" id="Gender">
<option value="">Select One</option>
<option value = "1">Female</option>
<option value = "2">Male</option>
</select> <input value="Add" type="submit" name="submit"> </form> </body>
</html>
在这里,<?php echo $_SERVER['PHP_SELF']?>
重定向您从您提交表单在同一页上。
您可以使用PHP的header()函数的
试试这个。我害怕我无法测试它,因为我没有在我的机器上安装Apache。
$con=mysqli_connect("centos-db.wireddog.local","customerinfo","password","customerinfo");
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO `customers` (`firstname`, `lastname`, `email`,
`phone`, `address1`, `address2`, `city`, `gender`) VALUES
('$_POST[firstname]','$_POST[lastname]',
'$_POST[email]','$_POST[phone]', '$_POST[address1]' ,'$_POST[address2]'
,'$_POST[city]' , '$_POST[gender]')"; if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "Customer Added"; mysqli_close($con);
header('Location: '.$_SERVER['PHP_SELF']); //Redirect to the same page
die;
p.s我不得不删除php的开关标签,因为SO似乎不喜欢在编辑器中渲染标签。
在''>''后面加上''到你的php文件的底部。它会在5秒后重定向到“http:// example.com/mypage.php”。 – 2015-01-21 09:59:38
完美的作品,非常感谢你! – user3243208 2015-01-21 10:02:40