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 do that?

Answers:

Answer

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

Answer

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.

Answer

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.

Answer

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.

Answer

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.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.