2014-02-12 51 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>"; 
    }