我正在尝试创建一个动态下拉框,一旦做出选择,该下拉框将访问MySQL。第二个框取决于第一个框的选择。我对javascript不太熟悉,但是遇到了一些代码,这些代码似乎是我想要的,但是没有执行,我也不知道为什么。代码的第一部分是javascript的选项字段,第二部分是dynamicdd.php。任何帮助都会很棒。谢谢。
<tr>
<td>Country: </td>
<td>
<select name="Countrybox" onchange="getlocation(this.value)">
<option value="none"> Please Select </option>
<?php
$qry2 = "Select Country from Locations";
$populate = mysqli_query($conn, $qry2);
while ($run = mysqli_fetch_assoc($populate)){
echo "<option value='".$run['Country']."'>".$run['Country']."</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td>Location:</td>
<td>
<select name="Locationbox" id="locationbycountry">
<option> Select Above First </option>
</select>
</td>
</tr>
<script type="text/javascript">
function getlocation(locationarea) {
var xhttp = new XMLHttpRequest();
var url = "dynamicdd.php";
var data = new FormData();
data.append('SearchValue', locationarea);
xhttp.open('POST', url, true);
xhttp.send(data);
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("locationbycountry").innerHTML = xhttp.responseText;
}
}
}
</script>
dynamicdd.php
<?php
if($_POST['SearchValue']){
$host = "localhost";
$username = "root";
$password = "";
$db = "Work";
$conn = mysqli_connect($host, $username ,$password, $db);
$choice = $_POST['SearchValue'];
$sql = "SELECT * FROM locations WHERE Country = '$choice'";
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
while ($row = mysqli_fetch_assoc($result)){
echo "<option value='".$row['Location']."'>".$row['Location']."</option>";
}
}
?>
答案 0 :(得分:0)
您的HTML格式足够好,但是您的PHP脚本中有错误。尝试使用以下代码来纠正一些简单的拼写错误:
<?php
if($_POST['SearchValue']){
$host = "localhost";
$username = "root";
$password = "";
$db = "";
$conn = mysqli_connect($host, $username ,$password, $db);
$choice = $_POST['SearchValue'];
$sql = "SELECT * FROM Locations WHERE country = '$choice'";
$result = mysqli_query($conn, $sql) or die('error');
while ($row = mysqli_fetch_assoc($result)){
echo "<option value='".$row['location']."'> ".$row['location']." </option>";
}
}
?>
此外,请确保更多地关注Select2,它是标准选择框的真正受支持的替代品。 https://select2.org/
答案 1 :(得分:0)
尝试与此更改javascript部分(它将引发消息框,并且您将看到脚本在结束前失败):
<script type="text/javascript">
function getlocation(locationarea) {
var xhttp = new XMLHttpRequest();
var url = "dynamicdd.php";
var data = new FormData();
data.append('SearchValue', locationarea);
xhttp.open('POST', url, true);
xhttp.send(data);
alert("*step 1*");
xhttp.onreadystatechange = function() {
alert("*step 2*");
if (xhttp.readyState == 4 && xhttp.status == 200) {
alert("*step 3*");
alert(xhttp.responseText);
document.getElementById("locationbycountry").innerHTML = xhttp.responseText;
}
}
alert("*step 4*");
}
</script>
<?php
if($_POST['SearchValue']){
$host = "localhost";
$username = "root";
$password = "";
$db = "";
$conn = mysqli_connect($host, $username ,$password, $db);
$choice = $_POST['SearchValue'];
$sql = "SELECT * FROM Locations WHERE country = '$choice'";
echo $sql;
}
else {
echo "value is not posted";
}
?>