[英]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.