【问题标题】:how to get selected dropdown item in php variable如何在php变量中获取选定的下拉项
【发布时间】:2014-02-12 05:57:08
【问题描述】:

我希望从 php 变量中的“list_cust_name”中选择的项目通过在 WHERE 子句中传递该 php 变量来通过该 sql 查询获取另一个下拉列表“list_cust_city”中的值..这是我的代码..请帮助我..

<td width="228">
    <label style="color:#000">Name </label>
    <?php
        $query_name = "SELECT DISTINCT cust_name FROM customer_db ORDER BY cust_name"; //Write a query
        $data_name = mysql_query($query_name);  //Execute the query
    ?>
    <select id="list_cust_name" name="list_cust_name">
        <?php
            while($fetch_options_name = mysql_fetch_assoc($data_name)) { //Loop all the options retrieved from the query
        ?> 
        <option value="<?php echo $fetch_options_name['cust_name']; ?>"><?php echo $fetch_options_name['cust_name']; ?></option>
        <?php
            }
        ?>
    </select>
</td>
<td width="250"> 
    <label style="color:#000">City </label>
    <?php
        $query_city = "SELECT DISTINCT cust_city FROM customer_db ORDER BY cust_city"; //Write a query
        $data_city = mysql_query($query_city);  //Execute the query
    ?>
    <select id="list_cust_city" name="list_cust_city">
        <?php
            while($fetch_options_city = mysql_fetch_assoc($data_city)) { //Loop all the options retrieved from the query
        ?> 
        <option value="<?php echo $fetch_options_city['cust_city']; ?>"><?php echo $fetch_options_city['cust_city']; ?></option>
        <?php
            }
        ?>
    </select>
</td>

【问题讨论】:

  • 无法解决您的实际问题...
  • 您要在此处刷新页面吗?或@Jokey 建议您在这里需要 ajax 来实现此目标
  • 如果我选择 list_cust_name 则不刷新,那么 list_cust_city 将自动出现选项

标签: php mysql sql drop-down-menu selecteditem


【解决方案1】:

您需要为此拨打AJAX

首先将JQuery 脚本添加到您的页面中的&lt;head&gt; 标签内,例如:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

那么你需要一个事件监听器来对你的第一个下拉列表进行 onchange,如下所示:

<script>
$('#list_cust_name').change(function(){
    $.ajax({
        url:'city.php',
        data:{cust_name:$( this ).val()},
        success: function( data ){
            $('#list_cust_city').html( data );
        }
    });
});
</script>

把上面的脚本放在你的&lt;body&gt;标签下面。

现在您要创建一个名为city.php 的页面,如上面的ajax 调用所示。您可以根据需要决定名称。

然后在该页面中您可以获得传递的值cust_name,您可以使用它执行查询。新的下拉选项可以生成如下。

city.php

<?php
    $cust_name=$_GET['cust_name'];      //passed value of cust_name
    $query_city = "your query with the cust_name"; //Write a query
    $data_city = mysql_query($query_city);  //Execute the query
    while($fetch_options_city = mysql_fetch_assoc($data_city)) { //Loop all the options retrieved from the query
    ?> 
        <option value="<?php echo $fetch_options_city['cust_city']; ?>"><?php echo $fetch_options_city['cust_city']; ?></option>
    <?php
    }   
?>

完成此脚本后,数据将传递回 AJAX 调用,并根据我们的代码放置在第二个下拉列表中。试试看

【讨论】:

  • 在创建 city.php 后,我应该为页面中的第二个下拉菜单编写代码吗??
  • 是的...您需要在主页中编写第二个下拉代码...只需添加&lt;select id="list_cust_city" name="list_cust_city"&gt;...否则jquery无法找到id为#list_cust_city的元素
【解决方案2】:

正如 Jokey 所说,你需要在这里执行 ajax...

直到选择客户名称都没有问题。

你必须像这样在 jquery(ajax) 中捕获 list_cust_name 的更改事件

JS 文件:

$('#list_cust_name').change(function(){
    var cust_name = $('#list_cust_name').val();
    $.post( 
             "./php/getCityNames.php",
             { cust_name: cust_name },
             function(data) {
                $('#list_cust_city').html(data);
             }

          );
 });

PHP 文件(名为 getCityNames.php):

<?php
$cust_name =  $_POST["cust_name"];  //getting the customer name sent from JS file
$query_city = "SELECT DISTINCT cust_city FROM customer_db WHERE cust_name = '$cust_name' ORDER BY cust_city"; //added where as per your requirement
    $data_city = mysql_query($query_city);  //Execute the query

foreach($categories as $category){  //Its my style
        echo "<option>";
        echo $data_city['cust_city'];
        echo "</option>";
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多