简体   繁体   English

将会话从jquery设置为php中的会话

[英]set session from jquery to session in php

i have a page in php to execute query. 我在php中有一个页面可以执行查询。 when i need the parameter from another page with session using jquery. 当我需要另一个页面的参数与会话使用jQuery。 so, how to set session in php from session in jquery. 因此,如何从jquery中的会话中在php中设置会话。 please check my code and give me solution. 请检查我的代码并给我解决方案。 thanks... 谢谢...

<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());  
?>

You can use ajax to send parameter to a php file like this 您可以使用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));
        }
    });

Note that both solutions needs you to check the input since the user could potentially modify it. 请注意,这两种解决方案都需要您检查输入,因为用户可能会对其进行修改。

Using Ajax 使用Ajax

The best way to get sDecode is probably to send it via jQuery.post() (AJAX) to a PHP page that will register the session value (after doing the appropriate checks to it). 获得sDecode的最好方法可能是通过jQuery.post() (AJAX)将其发送到PHP页面,该页面将注册会话值(对它进行适当的检查之后)。

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

and you get it in PHP via : 然后通过以下方式在PHP中获取它:

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

You might have to use .serialize() and then unserialize it in PHP depending on the content and how you treat it. 您可能必须使用.serialize() ,然后在PHP中根据内容及其处理方式对它进行反序列化。

Using Redirection 使用重定向

You can simply redirect to a page and transmit it directly as a GET value: 您可以简单地重定向到页面并将其直接作为GET值传输:

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

Then on the session.php page you would read it using this code : 然后在session.php页面上,您将使用以下代码阅读它:

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

You can have PHP fill in the Javascript variable from the session variable. 您可以让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