[英]Ajax JQuery post code with Php to query MySql database not working
I have an HTML form that runs a Javascript function on page load. 我有一个HTML表单,该表单在页面加载时运行Javascript函数。 In the Javascript function, a town name is sent via Ajax post call to Php function to get details from MySql database. 在Javascript函数中,城镇名称通过Ajax调用后发送给Php函数,以从MySql数据库获取详细信息。
Ajax call in JavaScript function JavaScript函数中的Ajax调用
var town = "Town name"; //depends on the previous execution in JS function
if (town != "undefined") {
jQuery.ajax({
type: "POST",
url: '../model/data_access/GetCity.php',
dataType: 'json',
data: {town: town},
success: function (obj) {
document.getElementById("selectedLocation").value = obj[0]["name"];
document.getElementById("placeDescriptionBox").value = obj[0]["description"];
}
});
}
GetCity.php GetCity.php
$town = $_POST['town'];
get_city($town);
function get_city($town)
{
$place = array();
$db_conn = DBConnection::get_database_connection();
$stmt = $db_conn->prepare("SELECT * FROM place WHERE name=?");
$stmt->bind_param("s", $town);
$stmt->execute();
if (!($result = $stmt->get_result())) {
echo "Error" . $stmt->error;
}
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
$place[0] = new Place();
$place[0]->set_name($row["name"]);
$place[0]->set_description($row["description"]);
echo json_encode($place);
}
}
The data from database are not displayed in the HTML form. 来自数据库的数据不会以HTML形式显示。 How can I solve this? 我该如何解决? I'm new to Ajax JQuery so any help is much appreciated. 我是Ajax JQuery的新手,因此非常感谢您的帮助。
You are making a POST request: 您正在发出POST请求:
type: "POST",
… but are trying to read the data from the query string: …但是试图从查询字符串中读取数据:
$town = $_GET['town'];
jQuery will post data in the request body when you make a POST request, so it will appear in $_POST
not $_GET
. 发出POST请求时,jQuery将在请求正文中发布数据,因此它将显示在$_POST
而不是$_GET
。
Change type: "POST",
to type: "GET",
(or omit it entirely as GET is the default). 更改type: "POST",
type: "GET",
(或完全忽略它,因为GET是默认设置)。
if you are checking whether the variable exist are not : 如果您正在检查变量是否存在,则不是:
if (town != "undefined") {} should be if (typeof town !== "undefined") {} if(town!=“ undefined”){}应该是if(typeof town!==“ undefined”){}
Change $town = $_GET['town'];
更改$town = $_GET['town'];
to $town = $_POST['town'];
到$town = $_POST['town'];
as you used type: "POST",
in your ajax request. 如您所使用的type: "POST",
在ajax请求中type: "POST",
。
Please pass the town name with url encoded with javascript function encodeURI and please debug the code whether your php code returns the town list or not 请传递城镇名称,并带有使用javascript函数encodeURI编码的url,并且请调试代码,无论您的php代码是否返回城镇列表
var town = "Town name"; //depends on the previous execution in JS function
if (town != "undefined") {
jQuery.ajax({
type: "POST",
url: '../model/data_access/GetCity.php',
dataType: 'json',
data: {town: town},
success: function (obj) {
console.log(obj);
document.getElementById("selectedLocation").value = obj[0]["name"];
document.getElementById("placeDescriptionBox").value = obj[0]["description"];
}
});
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.