2014-02-12 80 views
0

這裏是我的代碼,我做了一個腳本,通過在php中查詢,從「list_cust_name」中的選定項獲得「list_cust_city」中的值。我在城市的「list_cust_city」中沒有得到任何值。我製作了city.php在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> 
+0

需要在city.php下添加第二下降 –

+0

主頁''不需要 –

回答

0

PHP使用.到Concat的字符串。您的查詢更改爲:

$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 
    } 
?> 
+0

標籤我已經添加了完整的代碼,試試吧。我建議你爲客戶和城市使用ID而不是字符串值。 – Jokey

+0

做了仍然沒有得到的南非.. – Yash

+0

檢查您的請求。或者你能提供一些關於你的customer_db表結構的信息嗎? – Jokey

0

您必須使用文件準備好,因爲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); 
    } 
    }); 
    }); 
}); 
相關問題