0
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?

4 Answers 4

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'];
?>
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

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

set the GET parameter you sent to a session variable.

4 Comments

Ya..i put $_SESSION['id'] = $_GET['c'];and in another page i have retrive this value..if(isset($_SESSION['id'])) {$g['client_id']=$SESSION['id'];}..but it doesn't enters to the if condition.
Did you use session_start() on top of both the pages?
Check if the JS code is being able to send the data properly, and verify by echoing the SESSION variable just after assigning.
ohk, welcome. now you can mark the most useful answer as correct by ticking the small tickmark on the left.
1
$_SESSION['id'] = $_GET['c']

Just check c is really in the URL first.

3 Comments

I think it's actually $_GET['c'] no?
Ya..i put $_SESSION['id'] = $_GET['c'];and in another page i have retrive this value..if(isset($_SESSION['id'])) {$g['client_id']=$SESSION['id'];}..but it doesn't enters to the if condition.
Now it enters into if condition.i think problem is in $g['client_id']=$SESSION['id'];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.