php - 在 php 中查询下拉列表中的选定项目

标签 php jquery mysql sql drop-down-menu

这是我的代码,我编写了一个脚本,通过 php 中的查询从“list_cust_name”中的选定项目中获取“list_cust_city”中的值。我没有在“list_cust_city”中获得任何城市值。我制作了city.php

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

<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
        $customer=$fetch_options_name['cust_name'];
    ?> 
    <option value="<?php echo $fetch_options_name['cust_name']; ?>"><?php echo  $fetch_options_name['cust_name']; ?></option>
    <?php
        }
    ?>
</select>

city.php

<body>
    <?php
        include('dbconnect.php');
        db_connect();
        $cust_name1=$_GET['cust_name']; //passed value of cust_name
        $query_city = "SELECT DISTINCT cust_city FROM customer_db WHERE cust_name='$cust_name1'ORDER BY cust_city"; //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
        }   
    ?>
</body>

最佳答案

您必须使用文档就绪,因为 DOM 未加载。

$( document ).ready(function() {
  $('#list_cust_name').change(function(){
    alert("heyyy");
    $.ajax({
    url:'city.php',
    data:{cust_name:$( this ).val()},
    success: function( data ){
    $('#list_cust_city').html( data );
   }
  });
  });
});

关于php - 在 php 中查询下拉列表中的选定项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21722502/

相关文章:

javascript - 在 Javascript 响应中设置 session 变量

javascript - jQuery .data() 值中包含空格的问题

php - 无法连续对齐水平 slider

javascript - 基于单个输入字段的动画输出

java - 使用 JPA 插入的数据未反射(reflect)到连接表中

php - 如何使用 CodeIgniter 的 URL 参数将项目插入数据库?

mysql - 插入到mysql中

javascript - 如果数据由ajax填充,如何隐藏下拉列表

php - 检测 ajax 上传何时意外终止

php - 媒体查询正在删除社交按钮图标图像