开发者

How to pass JS variable to php?

I have a javascript functions which returns a hash. I need to pass this hash to php to do stuff with it. Whats the best way to 开发者_StackOverflow社区do that?


Assuming you mean JavaScript function returns a hash and sends it to PHP - then AJAX


You should give more info about what exactly you are trying to do. Like this all we can do is guess and you'll get no good answers. But the usual suspects in this case are:

  • AJAX (or JSON)

  • Cookies

  • Hidden form fields, where you set the value via JS

Give more info and we can be more specific.


You could use a cookie. How the exchange takes place (AJAX, page reload, whatever) is up to you.

in PHP: see setcookie()

in JS: see document.cookie - or perhaps a JS library such as Dojo / jQuery.


Use AJAX. But remember, never trust data coming in from GET or POST and always run the data through a security check before using or storing it.


look into jquery, this will make your this easier!

$.get('myphp.php?senddata='+javascriptdata,function(receivedata){

    alert('this is what was received' + receivedata);

});

or you could set a hidden input's value in a form and submit.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜