繁体   English   中英

如何在php变量中获取选定的下拉项

[英]how to get selected dropdown item in php variable

我希望从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>

您需要为此进行AJAX调用。

首先在页面的<head>标记内包含JQuery脚本,例如:

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

然后,您需要一个事件侦听器来更改第一个下拉列表,如下所示:

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

将上面的脚本放在您的<body>标签下面。

现在,您要创建一个名为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调用,并按照我们的代码将其放置在第二个下拉列表中。 试试看

就像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>";
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM