[英]Handling dynamic dropdown box using php and javascript
I am trying to create a dynamic drop down box that accesses MySQL once a selection has been made. 我正在尝试创建一个动态下拉框,一旦做出选择就访问MySQL。 The second box depends on the first box selection.
第二个框取决于第一个框选择。 I am not too familiar with javascript but I came across some code that seemed to be what I was looking for but is not executing and I don't know why.
我对javascript不太熟悉,但我遇到了一些似乎是我正在寻找的代码,但是没有执行,我不知道为什么。 The first part of the code is the option fields with the javascript and the second part is the dynamicdd.php.
代码的第一部分是带有javascript的选项字段,第二部分是dynamicdd.php。 Any help would be great.
任何帮助都会很棒。 Thank you.
谢谢。
<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 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>";
}
}
?>
Your HTML is formed well enough, but you have errors in your PHP script. 您的HTML形成得足够好,但您的PHP脚本中有错误。 Try the following code which corrects some simple typos:
请尝试以下代码来纠正一些简单的拼写错误:
<?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>";
}
}
?>
Also, please be sure to look more into Select2, which is a really well-supported replacement for standard select boxes. 此外,请务必仔细查看Select2,它是标准选择框的一个非常好支持的替代品。 https://select2.org/
https://select2.org/
try change javascript part with this (it will trow messagebox and you will see is script fail before end): 尝试用这个改变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";
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.