DGT
DGT

Reputation: 2654

decoding json value into a php variable

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!

Upvotes: 0

Views: 1718

Answers (1)

Tyler Carter
Tyler Carter

Reputation: 61597

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.

Upvotes: 3

Related Questions