Php AJAX不是&x27;不返回任何数据
对AJAX还是相当陌生的。我觉得我没有完全掌握它。我想向服务器提交部分数据,执行SQL查询并返回结果。这就是我到目前为止所做的: jQueryPhp AJAX不是&x27;不返回任何数据,php,jquery,mysql,ajax,json,Php,Jquery,Mysql,Ajax,Json,对AJAX还是相当陌生的。我觉得我没有完全掌握它。我想向服务器提交部分数据,执行SQL查询并返回结果。这就是我到目前为止所做的: jQuery j$('select[name=agent_Name]').change(function(event) { event.preventDefault(); var agentID = j$(this).val(); post_data = {'agent_ID':agentID}; console.log("Ab
j$('select[name=agent_Name]').change(function(event) {
event.preventDefault();
var agentID = j$(this).val();
post_data = {'agent_ID':agentID};
console.log("About to post data to the server");
j$.post('../include/booking_Modify.php', post_data, function(response){
if(response.type == 'AgDEpCd'){
output = response.text;
console.log(output);
}
if(response.type == 'error'){
output = response.text;
console.log(output);
}
}, 'json');
});
PHP
<?php
session_start();
require("../include/conn.php");
dbopen();
//check $_POST vars are set, exit if any missing
if(!isset($_POST["agent_ID"]))
{
$output = json_encode(array('type'=>'error', 'text' => 'Nothing was selected!'));
die($output);
}
$stmt = $conn->prepare("SELECT AgDEpCd FROM AGENTS WHERE AgentID = ?");
$stmt->bind_param('i', $_POST["agent_ID"]); // bind variables to the parameter
$stmt->execute();
$row = $result->fetch_assoc();
$AgDEpCd = $row['AgDEpCd'];
$stmt->close();
$output = json_encode(array('type'=>'AgDEpCd', 'text' => $AgDEpCd));
die($output);
?>
我检查以确保:
文件路径是正确的。
var agentID=j$(this.val();实际上获取了一个值,它确实如此
在PHPMyAdmin中手动输入SQL查询,以确保检索结果。
我似乎无法从服务器返回任何内容。我甚至不确定这是否可能。请帮忙 通常我只做回显和退出,短而快。在预先输入响应时,只需
console.log
并检查它是否返回任何响应。如果它不只是检查php代码,那么除了编码输出之外还有其他错误。试试看
<?php
session_start();
require("../include/conn.php");
dbopen();
//check $_POST vars are set, exit if any missing
if(!isset($_POST["agent_ID"]))
{
echo json_encode(array('type'=>'error', 'text' => 'Nothing was selected!'));
exit;
}
$stmt = $conn->prepare("SELECT AgDEpCd FROM AGENTS WHERE AgentID = ?");
$stmt->bind_param('i', $_POST["agent_ID"]); // bind variables to the parameter
$stmt->execute();
$row = $result->fetch_assoc();
$AgDEpCd = $row['AgDEpCd'];
$stmt->close();
echo json_encode(array('type'=>'AgDEpCd', 'text' => $AgDEpCd));
exit;
?>
未将结果分配给$result变量 应该是,
$result = $stmt->execute();
只需回显php脚本末尾的结果集。 它将被分配给ajax响应数据
$data = array('type'=>'AgDEpCd', 'text' => $AgDEpCd);
echo json_encode($data);
它缺少一个
]
。这是打字错误吗'select[name=agent\u name'
@Ghost Good catch.但是没有,我把前几行都打出来了。另外,在黑暗中,检查一下控制台.log(typeof response);并检查结果。或者只检查console.log(response)
只是一个建议:如果要测试输出,请将console.log放在外部,如果blocks必须在die之前尝试echo$output并将die$output更改为die();我添加了以下代码,但仍然没有看到任何返回的结果!