[英]Can't get ajax working with PHP to load a table of info from MySQL
So basically I have a drop down list that displays data from a MySQL table(accounts) that would display user accounts. 所以基本上我有一个下拉列表,该列表显示一个MySQL表(帐户)中的数据,该数据将显示用户帐户。 When the user selects one of the accounts I want it to display all facilities(facility table) that are owned by that account.
当用户选择一个帐户时,我希望它显示该帐户拥有的所有设施(设施表)。
I have the drop down displaying the accounts, but it will not run the onChange() function to load my table. 我有显示帐户的下拉列表,但不会运行onChange()函数来加载我的表。 Here is everything I have, can someone tell me why my function is not getting triggered at all?
这是我的一切,有人可以告诉我为什么我的功能根本没有被触发吗?
Index.php 的index.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';
?>
getaccounts.php getaccounts.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;
?>
This works how I need it to and displays all accounts within the drop down. 这可以满足我的需要,并在下拉菜单中显示所有帐户。 However I can't seem to get the showFacilities() function to work.
但是我似乎无法使showFacilities()函数正常工作。
getfacility.php getfacility.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>";
?>
footer.php (includes showFacilities()) footer.php(包括showFacilities())
<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 ©</p>
</footer>
</body>
</html>
Please tell me if I am doing this all wrong, am I laying everything out properly? 请告诉我我是否做错了所有的事情,我是否正确地布置了所有东西? Why is this function not being hit?
为什么没有点击此功能?
I have tried to a bunch of different things, and I just can't seem to get this to work, any help or advice or even a push in the proper direction will be very appreciated, thanks. 我尝试了很多不同的事情,但我似乎无法使它正常工作,非常感谢任何帮助或建议,甚至向正确方向的推动。
Your if else clauses don't add up (so your script is generating a script error, most likely a syntax error
). 您的if else子句不会累加(因此您的脚本正在生成脚本错误,很可能是
syntax error
)。
else{
xmlhttp.open("GET","getfacility.php?q="+account,true);
xmlhttp.send();
}
This piece doesn't have an IF
to accompany it. 这首歌没有
IF
陪伴。
This would be correct: 这是正确的:
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();
}
On a sidenote : Why create a form wrapper around your select
(the one that where you can load accounts) when you use an onchange
event to fire an XmlHTTPRequest
? 旁注 :当使用
onchange
事件触发XmlHTTPRequest
时,为什么要围绕select
(可以在其中加载帐户的对象)创建一个表单包装器?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.