从下拉列表中获取要在MySQL查询中使用的值


Get value from dropdown list to use in MySQL query

因此,我遇到了将HTML下拉列表中的值获取到变量中的问题,以便进行mysql查询。这将是一个有点棘手的解释,但我会尽我所能。(不要害羞地纠正我的英语或表达方式,这样问题会变得更具体、更容易理解)。

所以我有一个下拉列表,它从mysql查询中获取他的值。

<td>Designação atual :</td> <td><select name="desig_act" id="desig_act">
<?php
while ($row2 = mysql_fetch_assoc($result4)) {
echo "<option size=30 value=".$row2['new_name_freg'].">".$row2["new_name_freg"]."</option>";
}   
?>
</select>

此"连接"到此查询:

$sql4 = ("SELECT DISTINCT new_name_freg FROM freguesias WHERE codg_cc = '$pesq'");
$result4 = mysql_query($sql4, $link);

此查询将使用值填充下拉列表。我想做的是填充另一个下拉列表。例如,我选择一个国家的列表,当我选择国家时,它应该显示在其他下拉列表中的所有城市。

我一直在找男人。相信我,我有。

附言:当我看到你们向我展示了一种更好地解释问题的方法时,如果我把问题改了几次,请不要生气。对不起,如果我的英语不完美。谢谢你们的帮助。

您可以使用ajax和jquery来完成此操作。我试着写一个小例子

<!-- index.php -->
<select name="desig_act" id="desig_act">
<?php while ($row2 = mysql_fetch_assoc($result4)): ?>
   <option value="<?=$row2['new_name_freg']?>"> 
      <?=$row2["new_name_freg"]?>
   </option>
<?php endwhile; ?>
</select>
<!-- select tag for countries -->
<select name="country" id="country"></select>

编写一个小脚本以json 形式返回国家

<?php //ajax-countries.php
$link = mysql_connect(); // connect as usual
$query = ("SELECT * FROM countries");
$result = mysql_query($query, $link);
$json = array();
while ($row = mysql_fetch_assoc($result)) $json[] = $row;
echo json_encode($json);
?>

然后你可以有一些脚本,比如:

// script.js
$("#desig_act").change(function(){
  $.getJSON( "ajax-countries.php", function( data ) {
    $.each( data, function(key, val) {
      $("#desig_act").append("<option val='" + key + "'>" + val + "</option>");
    });
});

我希望它能对有用

1:创建一个PHP脚本以返回数据

本质上只是根据$_GET输入生成值。

2:在jquery中创建json请求

调用将返回数据的PHP文件,您将使用该数据向select添加更多值。

<?php
//Step 1 - The posted ajax data that will return our json request.
if(isset($_GET['fetchrow'])) //Is our ajax request called on page load? If yes, go to this code block
{
    //Other stuff like DB connection
    $pesq = mysql_escape_string($_GET['fetchrow']); //Put our variable as the variable sent through ajax
    $sql4 = ("SELECT DISTINCT new_name_freg FROM freguesias WHERE codg_cc = '$pesq'"); //Run our query
    $result4 = mysql_query($sql4, $link); //Please change from mysql_* to mysqli
    $data = array(); //The array in which the data is in
    while($row = mysql_fetch_assoc($result4)) //Look through all rows
    {
        array_push($data, $row); //Put the data into the array
    }
    echo json_encode($data); //Send all the data to our ajax request in json format.
    die; //Don't show any more of the page if ajax request. 
}
?>
<html>
    <head>
        <script type='application/javascript' src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-alpha1/jquery.min.js'></script> <!--Include jquery -->
        <script>
        //Step #2:
        //The jquery script calls ajax request on change of the first select
        $( "#desig_act" ).change(function() {
            $.getJSON('thisfilename.php', {fetchrow:$("#desig_act").val()}, function(data){ //Get the json data from the script above
                var html = '';
                var len = data.length;
                for (var i = 0; i< len; i++) { //Loop through all results
                    html += '<option value="' + data[i].new_name_freg + '">' + data[i].new_name_freg + '</option>'; // Add data to string for each row
                }
                $('#otherselect').html(html); //Add data to the select.
            });
        });
        
        </script>
    </head>
    <body>
    <!-- Your html code -->
    <td>Designação atual :</td> <td><select name="desig_act" id="desig_act">
    <?php
    while ($row2 = mysql_fetch_assoc($result4)) {
    echo "<option size=30 value=".$row2['new_name_freg'].">".$row2["new_name_freg"]."</option>";
    }   
    ?>
    </select>
    </td>
    <!-- The new select -->
    <select name='otherselect' id='otherselect'>
    </select>
    <!-- Rest of html code -->
    </body>
</html>