![](/img/trans.png)
[英]Passing php session variables from jquery, ajax to PHP thank you page
[英]SESSION value not passing after redirection to the thank you page
我正在嘗試通過$email
字段來表示感謝頁面,一旦用戶提交了查詢表單,重定向后就會出現該頁面。
它是一個兩步查詢表單,最后一頁是謝謝頁面。
SESSION似乎在“謝謝”頁面上直播,但是值丟失了。 我想在“感謝頁面”上將$email
字段發布到iframe。 請讓我知道會話ID到底在哪里出錯?
以下是代碼:
步驟1:小型查詢表格
<?php
error_reporting(0);
session_start();
require_once('validation.class.php');
if(isset($_REQUEST['btnSubmit']) == 'Next'){
$obj = new validation();
$obj->add_fields(trim($_POST['txt_fname']), 'req', 'Enter your first name.');
$obj->add_fields(trim($_POST['txt_contact']), 'req', 'Enter phone number.');
$obj->add_fields(trim($_POST['txt_finamount']), 'req', 'Enter the amount.');
$obj->add_fields(trim($_POST['sel_loantype']), 'req', 'Please select vehicle type.');
$error = $obj->validate();
if($error){
$error_msg = "".$error."";
$_SESSION['error_msgs'] = $error_msg;
header("location:".$_SERVER['HTTP_REFERER']."");
exit();
}else{
$_SESSION['form1data'] = $_REQUEST;
header("location: quick-quote.php");
exit();
/*$fname = trim($_REQUEST["txt_fname"]);
$surname = trim($_REQUEST["txt_surname"]);
$phone = trim($_REQUEST["txt_contact"]);
$finamount = trim($_REQUEST['txt_finamount']);
$sel_loantype = trim($_REQUEST['sel_loantype']);
$message = '<html><body>';
$message .= '<table rules="all" width="100%" style="border:1px solid #666;" cellpadding="10">';
$message .= "<tr><td><strong>First Name:</strong> </td><td>" . strip_tags($fname) . "</td></tr>";
if($surname != ''){
$message .= "<tr><td><strong>Surname:</strong> </td><td>" . strip_tags($surname) . "</td></tr>";
}
$message .= "<tr><td><strong>Phone:</strong> </td><td>" . strip_tags($phone) . "</td></tr>";
$message .= "<tr><td><strong>Amount to Finance:</strong> </td><td>" . strip_tags($finamount) . "</td></tr>";
$message .= "<tr><td><strong>Loan Type:</strong> </td><td>" . strip_tags($sel_loantype) . "</td></tr>";
$message .= "</table>";
$message .= "</body></html>";
$ToEmail = 'testemail@gmail.com';
$EmailSubject = "GET A QUICK QUOTE from ".strip_tags($fname);
$mailheader = "From: ".strip_tags($fname)."\r\n";
//$mailheader .= "Reply-To: ".$_REQUEST["txt_email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = $message;
if(@mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader)){
$_SESSION['sucess'] = "Your message has been sent successfully.";
$_SESSION['form1data'] = $_REQUEST;
header("location: quick-quote.php");
exit;
}else{
$_SESSION['sucess'] = "Sorry! Your message has not been sent.";
$_SESSION['form1data'] = $_REQUEST;
header("location: quick-quote.php");
exit;
}*/
}
}
?>
第2步代碼:
<?php
error_reporting(0);
session_start();
require_once('validation.class.php');
?>
<script type="text/javascript">
function submitToCRM()
{
$.ajax({
type: 'POST',
url: 'http://test.com.au/quick-quote/car-finance/quickquote-one.php',
data: $("#applynowform").serialize(),
beforeSend: function () {
$("#loadingimg").show();
},
success: function (){
//alert(data);
window.location.href = "http://www.test.com.au/thank-you";
}
});
步驟3:以上頁面將數據發送到quickquote-one.php表單處理腳本,該腳本具有以下代碼。
<?php
if(!isset($_SESSION))
{
session_start();
}
$_SESSION['user_email'] = $_POST['email'];
第4步:“謝謝”頁面(此頁面包含以下代碼)
<?php
if(!isset($_SESSION))
{
session_start();
$_SESSION['user_email'] = $_POST['email'];
echo $_SESSION['user_email'];
}
?>
加
session_set_cookie_params(0);
在你之前
session_start();
您還可以使用URL在頁面之間傳遞SID
(會話ID),以確保它不會在轉換過程中丟失。
url: 'http://test.com.au/quick-quote/car-finance/quickquote-one.php?<?php echo htmlspecialchars(SID); ?>',
和
window.location.href = "http://www.test.com.au/thank-you?<?php echo htmlspecialchars(SID); ?>";
您丟失會話是因為要將瀏覽器發送到下一個URL,而沒有相對路徑,而是一個完全限定的域。 這是一種安全措施,可防止將會話ID意外發送到錯誤的服務器。
另一個小解決方案是使用/page.php
之類的相對路徑,而不是http://www.domain.com/page.php
在第4步中,您的設置
$_SESSION['user_email']
再次。 如果表單已刷新,則您的帖子為空,並且您正在用一個空值覆蓋會話。 嘗試從步驟4中刪除它,然后離開。
<?php
if(isset($_SESSION['user_email']) && !empty($_SESSION['user_email']))
{
echo $_SESSION['user_email'];
}
?>
另外,如果未設置會話,您將嘗試回顯會話電子郵件值。 我認為,如果實際設置了會話,那將行不通。
我認為最好在一個頁面中啟動一個會話,並通過所有頁面相互連接訪問所有會話變量。 就像我們創建登錄頁面一樣。 當用戶登錄時,捕獲所有會話中的必需值,即使刷新后也可以在整個應用程序中對其進行訪問。
$query = "SELECT Useid,UserName,AccountStatus, FullName FROM Users WHERE UserName = :UserName";
通過此查詢,我們可以輕松獲取會話變量。
if($login_ok)
{
//for last visit
$Month = 2592000 + time();
//this adds 30 days to the current time
setcookie(AboutVisit, date("F jS - g:i a"), $Month);
//last visit ends here.
$_SESSION['user'] = $row['UserName'];
$_SESSION['userid'] = $row['Useid'];
$_SESSION['fullname'] = $row['FullName'];
}
在任何需要變量的地方都可以這樣使用
$username=$_SESION['user'];
我認為這將代替每個頁面中的每次啟動會議都起作用。 希望能幫助到你
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.