簡體   English   中英

將會話從jquery設置為php中的會話

[英]set session from jquery to session in php

我在php中有一個頁面可以執行查詢。 當我需要另一個頁面的參數與會話使用jQuery。 因此,如何從jquery中的會話中在php中設置會話。 請檢查我的代碼並給我解決方案。 謝謝...

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://code.ciphertrick.com/demo/jquerysession/js/jquerysession.js?d56ac9"></script>
<script>
$(document).ready(function(){ 
var sDecode = $.session.get("sesDecode"); 
alert(sDecode);      
}); 
</script> 
<?php
include('connection.php');  
$content= ==> how to get " sDecode " ???
$upd = mysql_query("UPDATE t_modules set content='$content' where id_t_modules='3'") or die(mysql_error());  
?>

您可以使用ajax將參數發送到這樣的php文件

$.ajax({
        url: "connection.php", 
        data: "Session="+$.session.get("sesDecode"), 
        type: "POST", 
        dataType: 'json',
        success: function (e) {
            console.log(JSON.stringify(e));
        },
        error:function(e){
            console.log(JSON.stringify(e));
        }
    });

請注意,這兩種解決方案都需要您檢查輸入,因為用戶可能會對其進行修改。

使用Ajax

獲得sDecode的最好方法可能是通過jQuery.post() (AJAX)將其發送到PHP頁面,該頁面將注冊會話值(對它進行適當的檢查之后)。

$.post('session.php', { d: sDecode }, function (response) {
      alert(response);
});

然后通過以下方式在PHP中獲取它:

$sDecode = $_POST["d"]; // Please test the input here!

您可能必須使用.serialize() ,然后在PHP中根據內容及其處理方式對它進行反序列化。

使用重定向

您可以簡單地重定向到頁面並將其直接作為GET值傳輸:

window.location.href=”session.php?d="+sDecode;

然后在session.php頁面上,您將使用以下代碼閱讀它:

$sDecode = $_GET["d"]; // Please test the input here!

您可以讓PHP從會話變量中填充Javascript變量。

<?php session_start(); ?>
<html>
...
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(document).ready(function() {
    var sDecode = <?php echo json_encode($_SESSION['sDecode']); ?>;
    alert(sDecode);
</script>

PHP在Jquery之前執行,因此在從會話獲取參數后使用ajax進行查詢

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM