views:

4055

answers:

5

Dear all I am using PHP and Javascript, My Javascript conatains function get_data()

      function get_Data(){
       var name;
       var job;
       .....

       return buffer;

     }

Now I have PHP with following

  <?php
  $i=0;
  $buffer_data; 

  /*here I need to get the Value from Javascript get_data() of buffer;
 and assign to variable $buffer_data*/

  ?>

How to assign the javascript function data into the PHP variable?

A: 

You would have to use AJAX as a client side script cannot be invoked by server side code with the results available on server side scope. You could have to make an AJAX call on the client side which will set the php variable.

What is this JS function doing, that cannot be done via php ?

Jobo
A: 

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

Demur Rumed
+4  A: 

Use jQuery to send javascript variable to your php file:

$url = 'path/to/phpFile.php';

$.get($url, {name: get_name(), job: get_job()});

In your PHP get your variables from $_GET['name'] and $_GET['job'] like this:

<?php
$buffer_data['name'] = $_GET['name'];
$buffer_data['job']  = $_GET['job'];
?>
Uzbekjon
using ajax will be ok?
garcon1986
@garcon1986, Yes, $.get() is using ajax!
Uzbekjon
A: 

A similar question already exists: How to pass a variable / data from javascript to php and vice versa?

Henrik Paul
A: 

If you don't have experience with or need Ajax, then just stuff your data into a post/get, and send the data back to your page.

J.J.