最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - use onclick to create session variable with jquery or javascript? - Stack Overflow

programmeradmin1浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论