0

Possible Duplicate:
How to get javascript function data into Php variable

I am writing a code in javaScript. in that I need to call the php function.

But the problem is again I have to pass the Javascript variable to the php function.

My javascrpt is as follows,(is this correct)

<script>
obj.value="0254"
if (obj2.value != -1) 
{
       var PLOptions= "<?php PLOptions(?>"+ obj.value +"<?php)?>"
}
</script>

Here PLOptions() is the php method and obj.value is the javascript variable.

please let me know, how can I achive this.

Community
  • 1
  • 1
Indu
  • 149
  • 2
  • 4
  • 9
  • 2
    The only way can do that is by using AJAX, as PHP is interpreted before the page is given to the user, and JavaScript is interpreted afterwards, thus the php code is no longer running. – Yet Another Geek May 20 '11 at 09:08
  • In php function with that variable what your going to do, because if you can do that in javascript means why to call php function – Deepika May 20 '11 at 09:10
  • In my code I have to call this php method(function) for the button click. but For that button I have called the Javascript function. – Indu May 20 '11 at 09:21

6 Answers6

4

You can't do that. PHP is executed server-side, Javascript client-side. What that means is, the PHP code is executed before the Javascript.

However, you could use AJAX to call a PHP function from Javascript.

skndstry
  • 678
  • 1
  • 7
  • 21
  • Actually For the Button click event I am calling the Javascript function(called save()). But inbetween the save() function I have to call a php function, that will get fetch the data from the data base. so how can I do it. – Indu May 20 '11 at 09:29
  • you have to use Ajax to do that. Here's a tutorial: http://www.xul.fr/en-xml-ajax.html#ajax-get-text – skndstry May 20 '11 at 14:30
1
  1. PHP runs on the server and sends HTML to the server.
  2. Server sends HTML to client.
  3. Client renders webpage and executes JavaScript

At this point it is too late to pass data to a PHP function.

You have to issue a new HTTP request…

  • Submit a form
  • Set location.href
  • Use Ajax
  • Add an image

… and include the data in the POST body or the GET query string.

PHP can then read from $_POST or $_GET and return some more data.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Actually For the Button click event I am calling the Javascript function(called save()). But inbetween the save() function I have to call a php function, that will get fetch the data from the data base. so how can I do it. – Indu May 20 '11 at 09:34
0

Note that they execute disjointly, when your PHP executes it's running server side, your JavaScript runs client side.

What you would have to do is post back/AJAX to the page and pass in the variable as part of the call.

Lloyd
  • 29,197
  • 4
  • 84
  • 98
0

It's not possible you must use Ajax request to send data to php scripts.

Headshota
  • 21,021
  • 11
  • 61
  • 82
0

Think you will have to use AJAX(HTTPRequest) to achive this and pass your parameters as POST or GET to php..

php code example:

if(isset($_POST['parameter1']) && isset($_POST['parameter2'])){

    //some function code

    echo return_value; //and here use echo as return to get the result to javascript

}

the AJAX part can be done in alot of ways, if you want me to extend with an example tell me if you are using a framework and if(which)

Johan Olsson
  • 608
  • 3
  • 10
0

JS is executed clientside while PHP is executed serverside, so you'll have to send the JS values to the server. This could possibly be tucked in $_POST or through AJAX

Harsh
  • 2,078
  • 6
  • 22
  • 37