我正在尝试使用下面的AJAX脚本将下拉ID传递给PHP以运行查询,但是看起来该变量实际上并未传递。当我对PHP文件进行硬编码时,查询运行正常,但是当我尝试动态地执行它时,查询返回“未定义”或根本没有。
AJAX代码
function ajax_post(){
var request = new XMLHttpRequest();
var id = document.getElementById("editorginfo").value;
alert (id);
request.open("POST", "parse.php", true);
request.setRequestHeader("Content-Type", "x-www-form-urlencoded");
request.onreadystatechange = function () {
if(request.readyState == 4 && request.status == 200) {
var return_data = request.responseText;
alert (return_data);
document.getElementById("orgeditname").value = return_data;
document.getElementById("orgeditphone").value = return_data;
}
}
request.send("id="+id);
}
PHP Parse Code
<?php
include_once('../php_includes/db_connect.php');
$searchid = $_POST['id'];
//$searchid = 1;
$sql = 'SELECT * FROM orginfo WHERE id = $searchid';
$user_query = mysqli_query($db_connect, $sql) or die("Error: ".mysqli_error($db_connect));
while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {
$orgid = $row["id"];
$orgname = $row["orgname"];
$orgphone = $row["orgphone"];
echo $orgname, $orgphone;
}
?>
不确定信息丢失的位置。当我提醒id时它正在捕获正确的信息,所以我认为问题出在我的发送部分,但我无法弄清楚我做错了什么。任何帮助将不胜感激。
提前致谢。
答案 0 :(得分:1)
您的请求标头错误。改变这一行 -
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
^^^^^^^^^^^^