admin管理员组

文章数量:1315258

Is it possible onclick to create session variable with javascript or jquery then use this variable with php ?

For example someone click link at the bottom

<div id="list"><a onclick = "" href="javascript:void(0);">+</a></div>

there will be created variable like number=1;

then I should call it in php like :

<?php $_SESSION["number"]; ?>

Is it possible onclick to create session variable with javascript or jquery then use this variable with php ?

For example someone click link at the bottom

<div id="list"><a onclick = "" href="javascript:void(0);">+</a></div>

there will be created variable like number=1;

then I should call it in php like :

<?php $_SESSION["number"]; ?>
Share Improve this question edited Nov 10, 2011 at 21:34 BerkErarslan asked Nov 10, 2011 at 20:07 BerkErarslanBerkErarslan 691 gold badge1 silver badge9 bronze badges 1
  • Session variables are stored server-side to uniquely identify the client. JavaScript only runs client-side anyway, so generating a session variable with it would be meaningless. – Blazemonger Commented Nov 10, 2011 at 20:09
Add a ment  | 

2 Answers 2

Reset to default 3

In order to pass a variable from JavaScript to PHP you have to make an ajax call. The easiest way is to use Jquery for that:

 <script type="text/javascript">
  function submitinfo(){
    $.ajax({
        type: "POST",
        url: "yourfile.php", 
        data: "values=" + $("#button1");        
    });

    }

  </script>

Also add a function to a button or something...

  <input type="button" id="button1" value="<?php print $values;>" onclick="submitinfo();"/>

Then in yourfile.php you simply get those values from $_POST['values'] and use wherever you want.

As others have pointed out you should not be creating session parameters with JavaScript, however if you need to get something from the client side to your server side, Ajax calls is the way to do it.

Sessions are maintained server side, it is not remended to create sessions client side, if it is very much necessary, you can call a server side script from client using any asynchronous libraries and have the server side code create session for you...

本文标签: phpuse onclick to create session variable with jquery or javascriptStack Overflow