2014-02-12 54 views
0

我想要從PHP變量「list_cust_name」中選定的項目,通過在該WHERE子句中傳遞該PHP變量,通過該SQL查詢在另一個下拉列表「list_cust_city」中獲取值。這裏是我的代碼..請幫助我..如何獲得所選的下拉項目在php變量

<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> 
+1

這裏你需要ajax。 – Jokey

+0

無法得到您的實際問題... –

+0

你想在這裏刷新頁面?或者@Jokey建議你在這裏需要ajax來達到這個效果 –

回答

0

您需要撥打AJAX

首先包括內部<head>標籤JQuery腳本到您的網頁,如:

<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> 

把上面腳本在您的<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調用完成後,它被放置在第二個下拉內按照我們的代碼。試試吧

+0

創建city.php後我應該在y ain頁面中編寫第二個下拉列表的代碼? – Yash

+0

你能幫我嗎... – Yash

+0

couldnt get u ....? –

0

像詼諧的說,U需要執行此阿賈克斯...

高達選擇客戶名稱是沒有問題的。

u必須捕捉list_cust_name變化事件在一個jQuery(阿賈克斯)這樣

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