我有以下下拉列表从数据库中获取列表联盟(此列表按预期填充)
<form style = "display: inline-block;" >
<select class="form-control" name ="coalition_select" onchange = "showCandidates(this.value)" method="GET">
<option id = "coalition_id" value="coalition">Coalitions</option>
<?php
include_once 'connection.php';
$sql_coalition = mysqli_query($conn, "SELECT coalition FROM candidates");
while ($row = $sql_coalition->fetch_assoc()) {
echo "<option value=\"coalition\">" . $row['coalition'] . "</option>";
}
?>
</select>
</form>
问题从这里开始。在这里,我试图首先从下拉列表中获取所选值(在联盟中),然后使用该值来显示具有类似联盟属性的用户。
这是从下拉列表中获取值的脚本:
<script>
function showCandidates (str) {
if (str.length == "") {
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 (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","includes/admin.users.list.inc.php?q="+str,true);
xmlhttp.send();
}
}
</script>
这是admin.users.list.inc.php文件
<body>
<?php
$q = $_GET['q'];
$conn = mysqli_connect('localhost','root','','voting');
if (!$conn) {
die('Could not connect: ' . mysqli_error($conn));
}
mysqli_select_db($conn,"osako_Voting");
$sql="SELECT * FROM candidates WHERE coalition = '".$q."'";
$result = mysqli_query($conn,$sql);
echo "<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Coalition</th>
<th>Email</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['coalition'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
</body>
问题似乎是我无法设置变量$q
以便它捕获所选值。现在,因为设置它似乎捕获索引而不是值本身。怎么能正确完成?如果它有任何帮助,我将使用本教程作为指导
https://www.w3schools.com/php/php_ajax_database.asp
IN SUMMERY: 如果我们有一个已使用php脚本动态填充的下拉列表。如何使用ajax获取所选值并在另一个php脚本中使用所述值。
谢谢
答案 0 :(得分:2)
问题出在这一行:
echo "<option value=\"" . $row['coalition'] . "\">" . $row['coalition'] . "</option>";
您将选择值传递给showCandidates(),以便通过AJAX传递,但您要将选项值设置为始终为静态&#34;联盟&#34;而不是您从DB获取的动态值。 也许你应该将行改为
<script type="text/javascript">
$(function(){
$(".select").click(function(){
$(".select .increase_div").animate({width:"500px"},1000)
})
})
</script>
<div class="select">
<div class="increase_div" style="width:100px;height:100px;background:gray;">
</div>
</div>
<hr>
<div class="select">
<div class="increase_div" style="width:100px;height:100px;background:gray;">
</div>
</div>
答案 1 :(得分:2)
您没有将value attribute
设置为coalition
。您只需为所有选项值设置常量字符串while ($row = $sql_coalition->fetch_assoc()) {
echo "<option value=\"$row['coalition']\">" . $row['coalition'] . "</option>";
}
http://10.24.120.110/mirrortin2/api/public/2ndlayer/search/[object%20Object],[object%20Object]/brandname/barcode/gender/producttype/productname
答案 2 :(得分:0)
我知道你接受了答案,但我对此感到困惑; 在你的功能中你有这个
document.getElementById("txtHint").innerHTML = "";
但是您没有任何带有"txtHint"
那么使用该ID对哪个元素进行排序?