1

Possible Duplicate:
How to pass JS variable to php?
pass a js variable to a php variable

I'm asking if it is possible passing value obtained through javascript function in a php function in the same file.for instance: I have a javascript function that calculate my position and pass this value to php function.is it possible?

[Edit]

Well I think I should be more specific...What I want to do specifically is to calculate my position through geolocation library of google API and return the value of latitude and longitude to a php variable in the way I can use it inside my php page. BUT this page it is not a normal php page it is a codeigniter Controller since I'm using MVC model to handle my project

Community
  • 1
  • 1
eng_mazzy
  • 1,049
  • 4
  • 23
  • 39
  • Requires an [AJAX request](http://stackoverflow.com/search?q=%5Bphp%5D+%5Bjavascript%5D+ajax+variable+to+php) – Michael Berkowski Feb 19 '12 at 13:52
  • possible duplicate of [How to pass JS variable to php?](http://stackoverflow.com/questions/3920209/how-to-pass-js-variable-to-php) and [Pass variable value from JS to PHP](http://stackoverflow.com/questions/8662976/pass-variable-value-from-js-to-php) and [pass a js variable to a php variable](http://stackoverflow.com/questions/4716177/pass-a-js-variable-to-a-php-variable) and [many others](http://stackoverflow.com/search?q=pass+javascript+variable+to+php). – Felix Kling Feb 19 '12 at 13:54
  • Can you provide more details. AJAX can do what you want, but if you will be posting a form or rerequesting the whole page then a hidden form field or cookie is more appropriate. – wheresrhys Feb 19 '12 at 13:55

5 Answers5

2

Yes it is, try to look into AJAX. With help from a library as jQuery and it's ajax methods should get you going.

Bas Slagter
  • 9,831
  • 7
  • 47
  • 78
0

To call back to a server-side script from the page, you can use an XMLHttpRequest (or a suitable wrapper such as jQuery).

James M
  • 18,506
  • 3
  • 48
  • 56
0

simpler way would be to create hidden form field and set its value in js and get it through php.

Vikram
  • 8,235
  • 33
  • 47
0

Yes, there are two ways

  • send the data directly using an ajax request, which doesn't require the whole page to be submitted
  • use javascript to write the value to a hidden form element/cookie which will then get passed to your php script (either in $_POST or $_COOKIE) when you request the page again
wheresrhys
  • 22,558
  • 19
  • 94
  • 162
-1

No, php is a server-side language as opposed to javascript, which is a client-side language. However, you can send an AJAX response with the position variable to a PHP script (try using the jQuery post function: http://api.jquery.com/jQuery.post/)

Jeroen
  • 13,056
  • 4
  • 42
  • 63