-7

Possible Duplicate:
How to pass JS variable to php?

How can I pass value from Javascript code to PHP variables.

Say we have this:

aa = $("#amount").text($("#progressbar").progressbar("option", "value")+"%");

declared in the <script> tag of the Javascript, I want this to be fetch or called in the PHP code.

Community
  • 1
  • 1
SMDC
  • 709
  • 1
  • 9
  • 17
  • 1
    [**So many duplicates!**](http://stackoverflow.com/search?q=pass+values+from+%22javascript%22+to+%22php%22+javascript) – Felix Kling Jan 03 '13 at 17:44
  • @Neal: Well, asking here on SO four hours earlier: [How to pass a value from javascript function?](http://stackoverflow.com/questions/14139955/how-to-pass-a-value-from-javascript-function) – hakre Jan 03 '13 at 19:12

3 Answers3

6

You need to use AJAX or a form to submit information to the server.

Naftali
  • 144,921
  • 39
  • 244
  • 303
2
$.ajax({ url: 'test.php',
     data: {action: 'aa'},
     type: 'POST',
     success: function(output) {
                         alert(output);
                    }
     });
Prasath K
  • 4,950
  • 7
  • 23
  • 35
Prashant16
  • 1,514
  • 3
  • 18
  • 39
2

JS is Client side , PHP is server side. So there is not a direct way to do the passing but you can do what you need to do by another method and AJAX is best option to it. Apart from this as an example, Following is the trick in which i am assiging screen width to my php varaible from javascript and I can echo its width by echo $width but actually the javascript code is being executing on echo and variable $width doesnot hold the value of screensize i.e in my case 1366 its actually hold the javascript code (<script> document.write(window.screen.width); </script>) thats being executed on echo .

$width = '<script> document.write(window.screen.width); </script>';
echo $width;
soft genic
  • 2,016
  • 3
  • 27
  • 44