简体   繁体   中英

set session from jquery to session in php

i have a page in php to execute query. when i need the parameter from another page with session using jquery. so, how to set session in php from session in jquery. 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({
        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

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).

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

and you get it in PHP via :

$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.

Using Redirection

You can simply redirect to a page and transmit it directly as a GET value:

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

Then on the session.php page you would read it using this code :

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

You can have PHP fill in the Javascript variable from the session variable.

<?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进行查询

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM