【发布时间】:2014-11-13 06:04:11
【问题描述】:
对于 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("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();实际上抓住了一个价值,它确实做到了 手动将 SQL 查询输入 PHPMyAdmin 以确保我正在检索结果。 我似乎无法从服务器返回任何东西。我不确定这是否可能。请帮忙!
【问题讨论】:
-
它缺少
]。这是错字吗?'select[name=agent_Name' -
@Ghost 不错。但是不,我打了前几行。
-
另外,只是在黑暗中拍摄,检查
console.log(typeof response);并检查结果。或者只是console.log(response) -
只是一个建议:如果您想测试输出,请将您的 console.logs 保留在如果块之外
-
在 die 之前尝试 echo $output 并将 die($output) 更改为 die();
标签: php jquery mysql ajax json