$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - How can I assign a javascript variable to the session value - Stack Overflow|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - How can I assign a javascript variable to the session value - Stack Overflow

matteradmin15PV0评论
function changeClient(s) {
if(s.value!=0)
{
    document.location.href = "map.php?c="+s.value;
}

I have to assign the value of s to a session variable $_SESSION['id'].How can i?

function changeClient(s) {
if(s.value!=0)
{
    document.location.href = "map.php?c="+s.value;
}

I have to assign the value of s to a session variable $_SESSION['id'].How can i?

Share Improve this question edited Jun 17, 2011 at 7:16 JiminP 2,1321 gold badge20 silver badges26 bronze badges asked Jun 17, 2011 at 7:13 AshithaAshitha 814 silver badges12 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 2

You can not assign client side variable(Javascript) to server side variable(PHP).

You have to use ajax to do this.

<script>
function assignJsValueToPHPSession()
{
 var jsVar = 1;
 $.ajax({
  type:post,
  url: "test.html",
  data: 'sessionjsvar=' + jsVar,
  success: function(){
    $(this).addClass("done");
  }
 });
}

test.php

<?php
  $_SESSION['phpvalue'] = $_POST['sessionjsvar'];
?>

You can set cookie using javascript and the same cookie will be accessible in server side variable(PHP).

$.cookie("name1", "test"); // emample 1
$.cookie("name1", "test", { expires: 7 }); // emample 2
$.cookie("name1", "test", { path: '/User', expires: 7 }); // emample 3

Get a cookie

alert( $.cookie("test") );

//In PHP

<?php
print_r($_COOKIE);
print)r($_REQUEST);
?>
$_SESSION['id'] = $_GET['c']

set the GET parameter you sent to a session variable.

$_SESSION['id'] = $_GET['c']

Just check c is really in the URL first.

Post a comment

comment list (0)

  1. No comments so far