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
-
Requires an AJAX requestMichael Berkowski– Michael Berkowski2012年02月19日 13:52:52 +00:00Commented Feb 19, 2012 at 13:52
-
possible duplicate of How to pass JS variable to php? and Pass variable value from JS to PHP and pass a js variable to a php variable and many others.Felix Kling– Felix Kling2012年02月19日 13:54:17 +00:00Commented Feb 19, 2012 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– wheresrhys2012年02月19日 13:55:58 +00:00Commented Feb 19, 2012 at 13:55
5 Answers 5
Yes it is, try to look into AJAX. With help from a library as jQuery and it's ajax methods should get you going.
Comments
To call back to a server-side script from the page, you can use an XMLHttpRequest (or a suitable wrapper such as jQuery).
Comments
simpler way would be to create hidden form field and set its value in js and get it through php.
Comments
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
$_POSTor$_COOKIE) when you request the page again
Comments
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/)