Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question

marked as duplicate by Felix Kling, Michael Berkowski, Vyktor, Evert, Graviton Feb 19 '12 at 14:24

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
yeah.. use AJAX!!! –  Vivek Chandra Feb 19 '12 at 13:52
    
Requires an AJAX request –  Michael Berkowski Feb 19 '12 at 13:52
    
    
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
    
I posted more specific –  eng_mazzy Feb 19 '12 at 14:07

5 Answers 5

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
share|improve this answer

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

share|improve this answer

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

share|improve this answer

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/)

share|improve this answer

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

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.