繁体   English   中英

在PHP下拉列表查询中选择的项目

[英]selected item in query fro dropdown list in php

这是我的代码,我编写了一个脚本,以通过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>

PHP使用. 连接字符串。 将查询更改为:

$query_city = 'SELECT DISTINCT cust_city FROM customer_db WHERE cust_name="'.$cust_name1.'"ORDER BY cust_city';

还将其添加到您的第一个php文件中:

<select id="list_cust_city" name="list_cust_city"></select>

这是完整的代码。

的PHP 1:

<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(function() {
 $('#list_cust_name').change(function(){
     $.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 $data_name = mysql_query("SELECT DISTINCT cust_name FROM customer_db ORDER BY  cust_name");?>

<select id="list_cust_name" name="list_cust_name">
<?php while($fetch_options_name = mysql_fetch_assoc($data_name)) { ?> 
<option value="<?php=$fetch_options_name['cust_name']; ?>"><?php=$fetch_options_name['cust_name']; ?></option>
<?php } ?>
</select>

<select id="list_cust_city" name="list_cust_city"></select>

city.php:

<?php 
  include('dbconnect.php');
  db_connect();
  $cust_name1=$_GET['cust_name'];
  $data_city = mysql_query('SELECT DISTINCT cust_city FROM customer_db WHERE cust_name="'.$cust_name1.'" ORDER BY cust_city');
  while($fetch_options_city = mysql_fetch_assoc($data_city)) {
    ?> 
    <option value="<?php=$fetch_options_city['cust_city'];?>"><?php=$fetch_options_city['cust_city'];?></option>
    <?php
  }   
?>

您必须使用准备就绪的文档,因为未加载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 );
   }
  });
  });
});

暂无
暂无

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

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