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?

share|improve this question
I have the same problem as you. Have you solved it ? – charles sun Dec 21 '09 at 14:37

5 Answers

up vote 8 down vote accepted

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'];
?>
share|improve this answer
using ajax will be ok? – charles sun Dec 21 '09 at 14:36
@garcon1986, Yes, $.get() is using ajax! – Uzbekjon Jan 29 '10 at 11:49

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 ?

share|improve this answer

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

share|improve this answer

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.

share|improve this answer
<script>
      function get_Data(){
       var name;
       var job;
       .....

       return buffer;

     }
function getData()
{
    var agree=confirm("get data?");
    if (agree)
    {
        document.getElementById('javascriptOutPut').value = get_Data();
        return true ;
    }else
    {
        return false ;
    }
}
</script>
<form method="post" action="" onsubmit="return getData()"/>
    <input type="submit" name="save" />
    <input type="hidden" name="javascriptOutPut" id="javascriptOutPut"/>
</form>
<?php
if(isset($_POST['save']))
{
    var_dump($_POST['javascriptOutPut']);
}
?>
share|improve this answer

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

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