使用php和javascript处理动态下拉框

我正在尝试创建一个动态下拉框,一旦做出选择,该下拉框将访问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>";

  }


}



 ?>


繁华开满天机
浏览 178回答 2
2回答

HUH函数

尝试与此更改javascript部分(它将引发消息框,并且您将看到脚本在结束前失败):&nbsp; &nbsp; <script type="text/javascript">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function getlocation(locationarea) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var xhttp = new XMLHttpRequest();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var url = "dynamicdd.php";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var data = new FormData();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data.append('SearchValue', locationarea);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xhttp.open('POST', url, true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xhttp.send(data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert("*step 1*");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xhttp.onreadystatechange = function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert("*step 2*");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (xhttp.readyState == 4 && xhttp.status == 200) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert("*step 3*");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert(xhttp.responseText);document.getElementById("locationbycountry").innerHTML = xhttp.responseText;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert("*step 4*");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; </script><?php&nbsp; &nbsp; if($_POST['SearchValue']){&nbsp; &nbsp; &nbsp; $host = "localhost";&nbsp; &nbsp; &nbsp; $username = "root";&nbsp; &nbsp; &nbsp; $password = "";&nbsp; &nbsp; &nbsp; $db = "";&nbsp; &nbsp; &nbsp; $conn = mysqli_connect($host, $username ,$password, $db);&nbsp; &nbsp; &nbsp; $choice = $_POST['SearchValue'];&nbsp; &nbsp; &nbsp; $sql = "SELECT * FROM Locations WHERE country = '$choice'";&nbsp; &nbsp; &nbsp; echo $sql;&nbsp; &nbsp; }&nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp;echo "value is not posted";&nbsp; &nbsp; }?>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript