0

I have try to pass a java script variable to PHP method from this way. But I have done wrong way. How do implement it in a correct way.

<script>
       function showMe(id){
           var name = <?php findDriverName(id,$drivers); ?>             
       }
</script>

The variable id is the Java Script Variable I need to pass this to PHP method findDriverName

nifCody
  • 2,394
  • 3
  • 34
  • 54
  • Why you cant use ajax? otherwise you need to submit a form and make a post request. – ecarrizo Jun 30 '15 at 02:49
  • 1
    possible duplicate of [How to pass data from Javascript to PHP and vice versa?](http://stackoverflow.com/questions/406316/how-to-pass-data-from-javascript-to-php-and-vice-versa) – Anonymous Jun 30 '15 at 02:52
  • can we call in line php function to ajax call. mean the ajax call and php function are in a same file – nifCody Jun 30 '15 at 03:02

1 Answers1

4

To get data to your PHP script, it has to be included in an HTTP request somehow. You can store it in a cookie, or do an AJAX request, or do a form POST/GET, but you have to have an HTTP request.

AJAX is your friend here.

dgrundel
  • 568
  • 3
  • 7
  • So there is no way to do it without ajax am i right? – nifCody Jun 30 '15 at 02:50
  • Could you please help me to do this task in ajax – nifCody Jun 30 '15 at 02:52
  • That's probably the _right_ way to do it in your case, based on the limited context I have. As I commented, you need to have an HTTP request involved, so your options are to post a form or do an AJAX call (which basically just posts a form anyway.) – dgrundel Jun 30 '15 at 02:52