php - 简单的 Ajax 过滤器不返回结果

标签 php mysql ajax filter

我正在尝试使用 W3 学校网站的代码通过下拉菜单实现存储在 MySQL 数据库中的数据的简单过滤器(请记住,我对 javaScript 非常陌生!)。但是 Ajax 脚本不返回任何结果。任何帮助表示赞赏。

ajax.html

<html>
<head>
<script>
function showUser(str) {
    if (str == "") {
        document.getElementById("txtHint").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("txtHint").innerHTML = xmlhttp.responseText;
             }
        }
        xmlhttp.open("GET","getuser.php?q="+str,true);
        xmlhttp.send();
    }
}
</script>
</head>
<body>

<form>
 <select name="genre" onchange="showUser(this.value)">
  <option value="">Select a genre:</option>
  <option value="1">clubbing</option>
  <option value="2">comedy</option>
  </select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here...</b></div>

</body>
</html>

获取用户.php

<!DOCTYPE html>
<html>
<head>
<style>
table {
    width: 100%;
    border-collapse: collapse;
}

 table, td, th {
    border: 1px solid black;
    padding: 5px;
}

th {text-align: left;}
</style>
</head>
 <body>

<script type = "text/javascript" src="ajax.html"></script>

<?php
$q = intval($_GET['q']);

 $con = mysqli_connect('localhost','root','','python');
 if (!$con) {
     die('Could not connect: ' . mysqli_error($con));
 }

 mysqli_select_db($con,"ajax");
$sql="SELECT * FROM info WHERE id = '".$q."'";
 $result = mysqli_query($con,$sql);

 echo "<table>
 <tr>
 <th>Venue</th>
 <th>Date</th>
 <th>Genre</th>
 </tr>";
 while($row = mysqli_fetch_array($result)) {
     echo "<tr>";
     echo "<td>" . $row['venue'] . "</td>";
     echo "<td>" . $row['datez'] . "</td>";
     echo "<td>" . $row['genre'] . "</td>";
     echo "</tr>";
 }
 echo "</table>";
 mysqli_close($con);
 ?>
</body>
</html>

最佳答案

找到了解决方案,如果有人遇到类似的问题......

function ajaxFunction(){
 var ajaxRequest;  // The variable that makes Ajax possible!

 try{
   // Opera 8.0+, Firefox, Safari
   ajaxRequest = new XMLHttpRequest();
 }catch (e){
   // Internet Explorer Browsers
   try{
      ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
   }catch (e) {
      try{
         ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
      }catch (e){
         // Something went wrong
         alert("Your browser broke!");
         return false;
      }
   }
 }
        // Create a function that will receive data 
 // sent from the server and will update
 // div section in the same page.
 ajaxRequest.onreadystatechange = function(){
   if(ajaxRequest.readyState == 4){
      var ajaxDisplay = document.getElementById('ajaxDiv');
      ajaxDisplay.innerHTML = ajaxRequest.responseText;
   }
 }
 // Now get the value from user and pass it to
 // server script.
 var gen = document.getElementById('gen').value;
 var queryString = "?gen=" + gen ;
 ajaxRequest.open("GET", "getuser.php" + 
                              queryString, true);
 ajaxRequest.send(null); 
}

关于php - 简单的 Ajax 过滤器不返回结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29396773/

相关文章:

python - Django 网站 - TemplateDoesNotExist 位于/edit/117/

php - 我需要从 textarea 插入一些文本,然后在输入时在数据库中更新它或使用 php ajax mysql 保存它

javascript - 提交表单而不点击更新或提交按钮

PHP 脚本从 POST 返回 responseText 到 AJAX?想要使用 Ajax POST 返回消息

php - PHP 中的 MySQL 到 JSON

java - 从 PHP 调用 .jar 文件 - Stanford NLP - 无法找到或加载主 java 类

php - 解决查询中的错误

c# - MySqlconnection 不包含 Connection、CommandText、Prepare 或 ExecuteNonquery 的定义

php - 在日期选择时从 mysql 检索数据

php - gwt ajax ssl登录