简体   繁体   中英

Grabbing JSON from a PHP file within separate JavaScript file

Needing to create a JavaScript file that sets values to two variables in a PHP file. That PHP file then creates a JSON structure and echoes some data.

I need a form in JavaScript where there is a form with the two inputs to assign to the PHP variables and then pull the JSON structure, and store the result in a HTML table.

Currently trying this:

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

Relevant PHP is:

$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']);

You'll know where the values are.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM