[英]PHP URL redirect using header
我正在尝试使用一个update_form.php
页面来处理网站的3个独立但相同的区域。 因此,例如:
我有3个数据页面: data1.php, data2.php, data3.php
。 全部链接到update_form.php。 提交update_form.php并将新数据存储在数据库中之后,我将使用什么代码重定向回相应的数据页面。
如果我要更新data1.php
,则在提交更新表单后,我想返回该页面。 需要使用PHP和MYSQL进行此操作。 任何帮助表示赞赏。
这是我在Dreamweaver中使用的代码。 您能帮我编辑做我上面提到的事情吗? 目前,无论我以什么形式输入,我都只能重定向到data1.php
:
$updateGoTo = "data1.php";
if (isset($_SERVER['QUERY_STRING'])) {
$updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
$updateGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $updateGoTo));
}
一种方法是使用Ajax。 您只需将数据发布到update_form.php并相应地更新页面。 您将不必处理重定向。
例如,使用jQuery会类似于以下内容:
$.post("update_form.php", { field1: "value1", field2: "value2" })
.done(function(data) {
// update your page here
});
如果您不想走那条路,您可以做的是将页面的URL以及其他表单参数一起传递。 您可以将其添加为表单中的隐藏输入。
<form action="update_form.php" method="POST">
<input type="hidden" value="[URL]" name="redirect_url">
<input type="text" value="OTHER VALUE">
...
</form>
然后在您的服务器上,您可以在$ _POST ['redirect_url']中检索URL并执行以下操作:
header('Location: ' . $_POST['redirect_url']);
只要确保清理redirect_url否则这可能是一个安全漏洞:)
通过使用php / mysql只需使用header();
如下
示例以data1.php具有以下代码
<?php session_start(); // session value to check the return msg from update_form.php
if(!empty($_SESSION['msg']){
echo '<div style="">'.$_SESSION['msg'].'</div>;
unset($_SESSION['msg']); // destroy msg after being visible to user
} ?>
<html>
<form action="update_form.php">
<input type="text" name="FullName" pattern="" />
<input type="submit" value="update" />
</html>
然后在update_form.php中,您将具有以下代码。
<?php session_start();
//Run your DB connection, eg i use mysqli
if(!empty($_POST)){
$name = mysqli_real_escape_string($link,$_POST['FullName'];
//run your update query successful
$msg = "Data updated successfully..";
}
else{
$msg = "Updation of data encounter error plz try again..";
}
$_SESSION['msg'] = $msg; // variable to return msg to user in other page
header("location: data1.php"); //this functin require page reload, if you know jquery is the best opt for this
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.