开发者

How can I assign a javascript variable to the session value

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜