gpt4 book ai didi

javascript - ajax、php 和 mysql 连接问题

转载 作者:行者123 更新时间:2023-11-29 22:17:47 27 4
gpt4 key购买 nike

我正在使用ajax从数据库中获取数据,但失败了。数据应该显示在我在主页中声明的名为“显示”的 div 中。查询运行但 div 上没有打印任何内容。这是我的代码:

//ajax:
<script>
function Jobs(str) {

if (str == "") {
document.getElementById("display").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange =
function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("display").innerHTML = xmlhttp.responseText;
}
}

xmlhttp.open("POST","search.php?q="+str);

xmlhttp.send();
}
}
</script>
//php
?php $q = intval($_GET['q']);?>

<?php

include('includes/conn.php');
$row="SELECT DISTINCT title,id FROM jobs WHERE dept='$q' AND state=0 ORDER BY id";
$query=mysqli_query($conn,$row) or die(mysqli_error($conn));
while($row=mysqli_fetch_array($query))
{
echo $row['title'];
}
mysqli_close($conn);
?>

最佳答案

示例ajax示例代码

addfield.php 文件:

<html>
<head>
<script>
function Jobs(str) {

if (str == "") {
document.getElementById("display").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange =
function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("display").innerHTML = xmlhttp.responseText;
}
}

xmlhttp.open("POST","search.php?q="+str);

xmlhttp.send();
}
}
</script>
</head>
<body>

<p><b>Start typing a name in the input field below:</b></p>
<form>
First name: <input type="text" onkeyup="Jobs(this.value)">
</form>
<p>Suggestions: <span id="display"></span></p>
</body>
</html>

search.php代码:

<?php
echo $q = $_REQUEST["q"];

?>

关于javascript - ajax、php 和 mysql 连接问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31042364/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com