Javascript ajax、php和mysql连接问题
我正在使用ajax从数据库中提取数据,但失败了。数据应该显示在我在主页中声明的名为“display”的div处。查询运行,但div上没有打印任何内容。以下是我的代码:Javascript ajax、php和mysql连接问题,javascript,php,jquery,mysql,ajax,Javascript,Php,Jquery,Mysql,Ajax,我正在使用ajax从数据库中提取数据,但失败了。数据应该显示在我在主页中声明的名为“display”的div处。查询运行,但div上没有打印任何内容。以下是我的代码: //ajax: <script> function Jobs(str) { if (str == "") { document.getElementById("display").innerHTML = ""; return; } else { if (
//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:
功能作业(str){
如果(str==“”){
document.getElementById(“显示”).innerHTML=“”;
返回;
}否则{
if(window.XMLHttpRequest){
//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}否则{
//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=
函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
document.getElementById(“display”).innerHTML=xmlhttp.responseText;
}
}
open(“POST”、“search.php?q=“+str”);
xmlhttp.send();
}
}
//php
?php$q=intval($_GET['q']);?>
示例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>
功能作业(str){
如果(str==“”){
document.getElementById(“显示”).innerHTML=“”;
返回;
}否则{
if(window.XMLHttpRequest){
//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}否则{
//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=
函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
document.getElementById(“display”).innerHTML=xmlhttp.responseText;
}
}
open(“POST”、“search.php?q=“+str”);
xmlhttp.send();
}
}
开始在下面的输入字段中键入名称:
名字:
建议:
search.php代码:
<?php
echo $q = $_REQUEST["q"];
?>
如果您使用的是jQuery
为什么不$。ajax
我的php文件是:它已经工作了。谢谢@Ramki。错误是因为“intval”。