0

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?

  • Do you mean, you have a JavaScript function which returns a hash, and then you would need to pass that hash to PHP? – Anthony Forloney Oct 13 '10 at 01:26
  • 2
    question and description are opposite :) – zod Oct 13 '10 at 01:27
  • possible duplicate of [How to pass a variable / data from javascript to php and vice versa?](http://stackoverflow.com/questions/406316/how-to-pass-a-variable-data-from-javascript-to-php-and-vice-versa) – outis Mar 09 '12 at 01:07

5 Answers5

4

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

Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
  • +1: Yup, AJAX ftw. Although it's not that hard to do it with just simple Javascript commands, I recommend that you use jQuery if you're comfortable with it because it keeps the familiar jQuery syntax and compatibility with you. – Kranu Oct 13 '10 at 01:43
1

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.

b_i_d
  • 180
  • 5
0

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.

pp19dd
  • 3,625
  • 2
  • 16
  • 21
0

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.

methodin
  • 6,717
  • 1
  • 25
  • 27
-1

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.

polyhedron
  • 1,560
  • 4
  • 19
  • 26
  • 1
    Come on... jQuery just for one single AJAX call? Are you trying to give the term "overdone" a whole new meaning? Nothing against jQuery, but it's not the holy grail for everything. – b_i_d Oct 13 '10 at 02:05
  • Well, I already use jquery, so this is a single line of code, vs using "raw js", so its good enough. –  Oct 13 '10 at 02:44
  • OK, if you already use jQuery in that project it's the obvious choice. As long as you don't include a whole library just to use one single function. ;) – b_i_d Oct 13 '10 at 02:51