从单独的JavaScript文件中的PHP文件中抓取JSON


Grabbing JSON from a PHP file within separate JavaScript file

需要创建一个JavaScript文件,将值设置为PHP文件中的两个变量。该PHP文件然后创建一个JSON结构并回显一些数据。

我需要一个JavaScript表单,其中有两个输入的表单分配给PHP变量,然后拉JSON结构,并将结果存储在HTML表中。

当前正在尝试:

$(document).ready(function() {
$("#button1").click(function(event){
$.get("external.php", {variable_1: 'VALUE1', variable_2: 'VALUE2'),           function(JSONresult){
        alert (variable_1);
}, 'json');
});
});

相关的PHP是:

$variable_1 = $_GET['variable_1'];
$variable_2 = $_GET['variable_2'];
// Some code in between that accesses a DB
echo json_encode($res->fetchAll());

Try

console.log(JSONresult); console.log(JSONresult['variable_1']);