【问题标题】:Redirect to URL after form post表单发布后重定向到 URL
【发布时间】:2013-10-11 05:07:57
【问题描述】:

我有一个我无法解决的问题。在我发布表单(单击按钮)后,我想重定向到一个 URL。我怎样才能做到这一点?到目前为止,我在互联网上没有找到解决方案。

我的文件:

addmatch.html

<html>
<body>
<form action="insert.php" method="post">
Date: <input type="text" name="date">
Home: <input type="text" name="home">
Away: <input type="text" name="away">
Result: <input type="text" name="result">
<input type="submit" name="submit">
</form>

</body>
</html>

插入.php

<?php

define("HOST", "localhost");
define("DBUSER", "..");
define("PASS", "..");
define("DB", "..");

$con=mysqli_connect(HOST, DBUSER, PASS, DB);

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="INSERT INTO wedstrijden (date, home, away, result)
VALUES
('$_POST[date]','$_POST[home]','$_POST[away]','$_POST[result]')";

if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "1 record added";

mysqli_close($con);
?>

提前致谢!

【问题讨论】:

  • 使用 header('Location: ur_path');
  • 人们使用 javascript 来做到这一点。使用类似window.location.assign("your-url-goes-here")
  • @zander 这取决于个人情况,有时 PHP 可能更合适。 IE。 Javascript 可以被禁用。
  • 哦,那么 header(..) 应该这样做
  • 危险:你容易受到SQL injection attacks的影响,你需要defend来对抗。

标签: php forms url post redirect


【解决方案1】:

正如我现在所看到的,您的表单将发送 post 请求到 insert.php 如果要在将数据保存在数据库中后重定向用户,则应添加 header("location youUrl.php");在任何输出(回显、打印等)之前

【讨论】:

    猜你喜欢
    • 2015-09-12
    • 2012-08-19
    • 2018-05-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-25
    • 1970-01-01
    • 2011-07-07
    • 1970-01-01
    相关资源
    最近更新 更多