为什么我的AJAX无法加载到我的网页上?

时间:2019-04-03 10:58:30

标签: php html ajax

我已经设置了JavaScript和AJAX,它们应该从受一组过滤器约束的数据库中获取数据,并返回与那些过滤器匹配的结果表。我以为我已经正确设置了代码,但是当我提交搜索过滤器表单时,页面会重新加载,并且什么也没有发生。我无法弄清楚我要去哪里错了!

connection.php只是一个包含我的数据库详细信息/用户名/密码的文件,我知道它可以正常工作。我已经仔细检查了语法错误,但是找不到任何错误,因此这一定与我编写代码的方式有关。但是,我对所有这些都还很陌生,因此我真的很想知道我做错了什么。任何帮助将不胜感激!

我的JavaScript:

 function showStock(search, genre, publisher, minyear, maxyear) {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            document.getElementById("results").innerHTML = this.responseText;
        }

        xmlhttp.open("GET", "stock.php?search=" + search + "&genre=" + genre + "&publisher=" + publisher +
            "&min-year=" + min - year + "&max-year=" + max - year, true);
        xmlhttp.send();
    }
}

我的过滤器形式:

<form id="filters">
    <label> Search by Title:</label><br>
    <input type="search" name="search">
    <label>Genre:</label><br>
    <select name='genre'>
        <option value='All'> All </option>
        <?php
            function dropdownOptions($category) {
                require('connection.php');
                $sql = "SELECT DISTINCT ".$category." FROM stock";
                $result = $conn->query($sql);
                while($row = $result->fetch_assoc()) {
                    echo "<option value='" . $row[$category] . "'>" . $row[$category] . "</option>";
                }
            }
            dropdownOptions("genre");
        ?>
    </select>
    <label>Publisher:</label><br>
    <select name="publisher">
        <option value="all"> All</option>
        <?php
            dropdownOptions("publisher");
        ?>
    </select>
    <div class="year">
        <label>Release Year:</label><br>
        <input  type="number" name="min-year" min="1970" max="2018">
        <label>&nbsp; to &nbsp;</label>
        <input  type="number" name="max-year" min="1970" max="2018">
    </div>
    <div class="buttons">
        <button type="submit" onclick="showStock(search, genre, publisher, min-year, max-year)">SEARCH</button>
        <button type="reset">CLEAR</button>
    </div>
</form>

我的stock.php代码:

<?php
    require('connection.php');
    $search = $_GET['search'];
    $publisher = $_GET['publisher'];
    $genre = $_GET['genre'];
    $minyear = intval($_GET['min-year']);
    $maxyear = intval($_GET['max-year']);

    if ($search == "") {
        $searchQ = "";
    } else {
        $searchQ = "AND CONTAINS(title, $title) ";
    }

    if ($genre == "all") {
        $genreQ = "";
    } else {
        $genreQ = "AND genre = $genre ";
    }   

    if ($publisher == "all") {
        $publisherQ = "";
    } else {
        $publisherQ = "AND publisher = $publisher ";
    }

    if ($minyear == "" && $maxyear == "") {
        $yearQ = "";
    } else if ($minyear == "" && $maxyear != "") {
        $yearQ = "AND release_date <= $maxyear ";
    } else if($minyear != "" && $maxyear == "") {
        $yearQ = "AND release_date >= $minyear ";
    } else if ($minyear != "" && $maxyear != ""){
        $yearQ = "AND release_date BETWEEN $minyear AND $maxyear ";
    }

    $sql = "SELECT * FROM stock WHERE id > 0".$searchQ.$genreQ.$publisherQ.$yearQ.";";
    $result = $conn->query($sql);

    echo "<table>
            <tr>
              <th>ID</th>
              <th>Title</th>
              <th>Publisher</th>
              <th>Genre</th>
              <th>Price</th>
              <th>Release Year</th>
              <th>Stock Units</th>
            </tr>";
    while($row = mysqli_fetch_array($result)) {
        echo "<tr>";
        echo "<td>" . $row['id'] . "</td>";
        echo "<td>" . $row['title'] . "</td>";
        echo "<td>" . $row['publisher'] . "</td>";
        echo "<td>" . $row['Hometown'] . "</td>";
        echo "<td>" . $row['genre'] . "</td>";
        echo "<td>" . $row['price'] . "</td>";
        echo "<td>" . $row['release_date'] . "</td>";
        echo "<td>" . $row['stock_units'] . "</td>";
        echo "</tr>";
    }
    echo "</table>";
    mysqli_close($con);
?>

我希望将相关结果表加载到我的网页上(托管在我的uni服务器上,位于https://students.emps.ex.ac.uk/admt201/webdev/stock-lookup.php上),但是发生的所有事情是,我的搜索参数被添加到了原始网页的url中,并且没有新的负荷。

1 个答案:

答案 0 :(得分:0)

这不是答案,而是带有多个建议的冗长评论。

最好通过分解并确认每个部分来解决此类问题。由于众所周知,PHP很难对进行故障排除(因为如果出现错误,它不会产生任何有用的信息,它只会异常终止)。我建议您首先解决PHP方面的问题。

因此,请复制您的stock.php文件,并用一些硬编码的数据替换$_GET[]变量赋值。然后,确保它输出成功消息(以确保您接收到期望的数据),并以独立方式运行修改后的stock.php文件。 您首先需要确认PHP是否正常运行,因为如果不能运行,则可以对javascript进行故障排除,直到牛们回家,真正的幸福将继续困扰着您。

在对PHP进行故障排除时,另一种有用的故障排除方法是在整个文件中简单地添加一些文件写入,如下所示:

$hF = fopen("__debug.log", "a");
//run a couple lines of code
fwrite($hF, "Got to here 01");
//run a few more lines of code
fwrite($hF, "Got to here 02");
//repeat...

然后,检查创建的__debug.log文件,查看是否已完成所有预期的文件写入,或者代码是否在途中死亡。否则,你怎么知道?

请注意,有一个extension for Chrome called PHPConsole允许您将控制台消息输出到浏览器,但是从头开始可能会有些棘手-而上面的fwrite()方法是固定的-可靠可靠且易于实施。

类似地,在javascript方面,用一堆console.log()语句对代码加盐,以找出问题的根源。