0

When I pass the results from getJSON to parseInfo() function just like the one below, is it possible to get results back into a php variable so that I could put the latter through another php function.

$.getJSON('getinfo.php', { id:id }, parseInfo);

function parseInfo(data) {
   <?php 
      $some_var = json_decode(data);
      function some_function($some_var) {
         // rest of the script here...
      } 
   ?>
}

Can anyone please help me out with this? I would really appreciate it.
Cheers!

1 Answer 1

3

PHP runs BEFORE the page is sent. Javascript runs AFTER the page is sent. Therefore the only way to can run PHP is to request a page.

So, if you wanted to pass data to PHP, you would have to call another page like ajax.php:

<?php

$data = $_POST['data'];
// ... do stuff ...

?>

From your script:

$.post('ajax.php', data);

See this question.

Sign up to request clarification or add additional context in comments.

3 Comments

Wow. Thanks a lot for the quick reply. I will give it a shot. Best,
I'm sorry but may be I wasn't so clear earlier. I can't seem to achieve the thing that I want to with the way you've suggested. Firstly, because the post function (below) doesn't seem to do anything. Secondly, the whole purpose of doing this is just so a php function can format the content of one of the array element (data.desc) returned from the line: json_encode($ref[$id]) within the 'getinfo.php' script. So, the idea is to print the formated data.desc inside the same #div, like so: $('div#info').html(data.desc); // i tried function parseInfo(data) { $.post("ajax.php",data); } Thanks
Oh, I plum forgot. The $.post() thing is part of jQuery, a very popular Javascript framework. Given you might already use it, you have to get the data the page outputs via a function passed in the called. $.post('ajax.php', data, function(data){ alert(data); });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.