无法让 ajax 使用 PHP 从 MySQL 加载信息表


Can't get ajax working with PHP to load a table of info from MySQL

所以基本上我有一个下拉列表,显示来自MySQL表(帐户(的数据,该表将显示用户帐户。当用户选择其中一个帐户时,我希望它显示该帐户拥有的所有设施(设施表(。

我有显示帐户的下拉列表,但它不会运行 onChange(( 函数来加载我的表。这是我所拥有的一切,有人可以告诉我为什么我的函数根本没有被触发吗?

索引.php

<?php
    require_once('sessionstart');
    require_once('header.php');
    require_once('dbconn.php');
    //Accounts
    require_once('getaccounts.php');
    //Facility
    echo "<div id='facilities'>";
        require_once('getfacility.php');
    echo "</div>";
?>

<?php
    require_once 'footer.php';
?>

获取帐户.php

<?php
//require files
require_once('sessionstart.php');
require_once('dbconn.php');
//clear options variable
$options = "";
//connect to db and test connection.
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);        
if (!$dbc) {
    die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT account_id, account_name FROM accounts";
$data = mysqli_query($dbc, $sql);
//loop through data and display all accounts
while ($row = mysqli_fetch_array($data)) {
         $options .="<option>" . $row['account_name'] . "</option>";
}
//account drop down form
$accountDropDown="<form id='account' name='account' method='post' action='getaccounts.php'>
                    <label>Accounts: </label>
                    <select name='account' id='account' onchange='showFacilities(this.value)'>
                        <option selected='selected' disabled='disabled' value=''>Select account</option>
                    " . $options . "
                    </select>
                </form>";
//echo out account form
echo $accountDropDown;
?>

这可以按照我的需要工作,并在下拉列表中显示所有帐户。但是我似乎无法让 showFacilities(( 函数工作。

获取设施.php

<?php
require_once('dbconn.php');
$q = intval($_GET['q']);
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);        
if (!$dbc) {
    die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM facility "
     . "INNER JOIN accounts ON accounts.account_id = facility.account_id "
     . "WHERE facility.account_id = '".$q."'";
$data = mysqli_query($dbc, $sql);
echo   "<table>
        <tr>
        <th>Facility Number</th>
        <th>Facility Name</th>
        <th>Facility Address</th>
        <th>Facility City</th>
        </tr>";
    //loop through data and display all accounts
    while ($row = mysqli_fetch_array($data)) {
        echo "<tr>";
        echo "<td>" . $row['facility_number'] . "</td>";
        echo "<td>" . $row['facility_name'] . "</td>";
        echo "<td>" . $row['facility_address'] . "</td>";
        echo "<td>" . $row['facility_city'] . "</td>";
        echo "</tr>";
    }
    echo "</table>";
?>

页脚.php(包括显示设施(((

<script>
    function showFacilities(account){
        //I wrote this to test and see if this function was even being triggered.
        document.alert("test");
        if(account == ""){
            return;
        }
        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("facilities").innerHTML = xmlhttp.responseText;
            }
        }
        else{
            xmlhttp.open("GET","getfacility.php?q="+account,true);
            xmlhttp.send();
        }
    }
</script>
        <footer>
                <p>Copyright &copy</p>
        </footer>
    </body>
</html>

请告诉我,如果我做错了这一切,我是否正确地布置了所有内容?为什么这个功能没有被命中?

我已经尝试了很多不同的事情,但我似乎无法让它工作,任何帮助或建议,甚至是朝着正确方向的推动都将不胜感激,谢谢。

你的 if else 子句没有加起来(所以你的脚本正在生成脚本错误,很可能是syntax error(。

    else{
        xmlhttp.open("GET","getfacility.php?q="+account,true);
        xmlhttp.send();
    }

这件作品没有IF陪伴它。

这是正确的:

    if(account == ""){
        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("facilities").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET","getfacility.php?q="+account,true);
        xmlhttp.send();
    }

附带说明:当您使用 onchange 事件触发XmlHTTPRequest时,为什么要围绕您的select(您可以在其中加载帐户(创建一个表单包装器?