【发布时间】:2016-03-10 21:06:12
【问题描述】:
我再次需要帮助。我正在尝试以 JSON 格式从 AJAX 返回表格的多行,并将这些值放在文本框中。这是我的代码。
HTML:
$.ajax({ /* START AJAX */
type: "POST",
url: "actionprt.php",
data: {"loadnumber": loadnumber},
dataType: 'json',
success: function(data) {
$("#CustomerName").val(result.Customer);
$("#CustomerAddress").val(result.Address);
}
}); /* END OF AJAX */
$.ajax({ /* START AJAX */
type: "POST",
url: "actionprt2.php",
data: {"loadnumber": loadnumber},
dataType: 'json',
success: function(data) {
$("#prtdescription1").val(result.data[0]);
$("#prtmethod1").val(result.data[1]);
}
}); /* END OF AJAX */
PHP:
$stmt = $con->prepare("SELECT `tblExpDescription`.`Description`,`Method`,`PONumber`,`Gallons`,`DollarAmount` FROM `tblExpenses` LEFT JOIN `tblExpDescription` ON (`tblExpenses`.`Description` = `tblExpDescription`.`ExpenseID`) WHERE (`tblExpenses`.`LoadID`= ?);");
$stmt->bind_param("i", $_POST["loadnumber"]); /* PARAMETIZE THIS VARIABLE TO YOUR QUERY */
$stmt->execute(); /* EXECUTE QUERY */
$stmt->bind_result($Description, $Method, $PONumber, $Gallons, $Amount);
/* BIND THE RESULTS TO THESE VARIABLES */
$stmt->fetch(); /* FETCH THE RESULTS */
$stmt->close(); /* CLOSE THE PREPARED STATEMENT */
/* RETURN THIS DATA TO THE MAIN FILE */
echo json_encode(array("Description" => $Description, "Method" => $Method, "PONumber" => $PONumber, "Gallons" => $Gallons, "Amount" => $Amount));
}
这将返回一行,但可以有 0 行或多行。如何将此数组返回到表单?
【问题讨论】:
标签: php jquery arrays json ajax