繁体   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