Reputation: 3035
I've got a simple question, but haven't been able to find the exact solution I need. How can I use a jQuery $.ajax to call a PHP file that just echos two PHP variables, and save them to javascript variables in the response?
Upvotes: 3
Views: 1580
Reputation: 19888
you would do something like:
$.getJSON('ajax_responder.php', function(data){
window.var1 = data.var1;
window.var2 = data.var2;
});
and then in ajax_responder.php
<?php
header('Content-type: application/json');
$var1 = 'foo';
$var2 = 'bar';
$data = array(
'var1' => $var1,
'var2' => $var2
);
echo(json_encode($data));
?>
see http://api.jquery.com/jQuery.getJSON/
Upvotes: 6