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

Passing JavaScript Variable to PHP Session variable - Stack Overflow

programmeradmin4浏览0评论

I am trying to update/create session variable when I change the option thru a drop-down box. TThe value is stored in a javascript function. Can any1 tell me how I can pass this variable to my PHP session variable without using a AJAX request. Thanks

I am trying to update/create session variable when I change the option thru a drop-down box. TThe value is stored in a javascript function. Can any1 tell me how I can pass this variable to my PHP session variable without using a AJAX request. Thanks

Share Improve this question asked Jan 20, 2011 at 5:44 HimanshuHimanshu 1,4434 gold badges24 silver badges36 bronze badges 1
  • I would just cast level 9 jquery at this problem, personally. – goat Commented Jan 20, 2011 at 6:09
Add a ment  | 

5 Answers 5

Reset to default 3

No ajax? ok,

var img = new Image();
img.src = "script.php?myVal=" + encodeURIComponent(theValueToPass);

The browser will try to load the image by requesting the url. Obviously it will be a fools errand, but your php script will get executed and the value available in php's $_GET['myVal']

Hmmm, I think, You can do it by adding a cookie value with your javascript, and update your session in php on the NEXT PAGE REFERESH, with reference to that cookie value.

I haven't tried it, and i don't think there is way we can update the session from just client side with javascript.

Thanks

You can't really.

Session data is altered with the server, while Javascript can only alter client-side data (such as cookies). You can, however, create a solution by creating a new PHP page that passes a variable within the GET/POST request, then alters the session.

The only alternative I can think of is using javascript to set a parameter in the URL which you could interpret later with PHP, but that would be after another page load.

http://www.mine./index.html?paramter=variable&anotherparam=anothervariable

see this post for a method to achive this

edit: I don't think this will achive what you after on second thought, but I'll leave the answer just in-case

PHP session variable resides on the server:

http://www.w3schools./PHP/php_sessions.asp

but the JavaScript values are on client-end only.

There should be a mechanism to send this to server, if not using ajax: try saving this value into a hidden field and then sending it using usual get/post.

发布评论

评论列表(0)

  1. 暂无评论