使用带有ajax的PHP,根据以前的下拉列表选择,从数据库填充下拉列表数据

时间:2014-04-20 20:29:43

标签: php ajax

我想要的结果显示为 1。http://www.vaaah.com/php/view/DropDownList/13/How-to-populate-a-drop-down-based-on-previous-drop-down-selection-using-PHP-with-ajax 2。http://www.sourcecodester.com/tutorials/php/5568/creating-dependent-dropdown-list-php-jquery-and-ajax.html

我试过了。但它不起作用。谁能告诉我哪里出错了? 感谢您的帮助〜

首先我有Main.php

<?php
require("db_connection.php");
?>
<!doctype html>
<html>
 <head>
 <meta charset="utf-8">
<title>Dependent DropDown List</title>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {

$("#state").change(function() {
    $(this).after('<div id="loader"><img src="img/loading.gif" alt="loading     subcategory" /></div>');
    $.get('ajax-city.php?state=' + $(this).val(), function(data) {
        $("#city").html(data);
        $('#loader').slideUp(200, function() {
            $(this).remove();
        });
    }); 
 });

 });
 </script>
 </head>
 <body>

<form action="" method="get">
<fieldset>  
<legend style="color:black">Address Detail</legend> 
<table>

<tr>
<td>Country</td>
<td> :</td>
<td> <select name="country" id="country" >
         <option value="">--- Select ---</option>
            <?php
             if (isset ($db1)&&$db1!=""){

                }
             ?>
            <?php
                 $list=mysql_query("select * from delivery group by country     ");
                    while($row_list=mysql_fetch_assoc($list)){
            ?>
                    <option value="<?php echo $row_list['country']; ?>" <?php   if($row_list['country']==$db1){echo "selected"; } ?>>
                                 <?php echo $row_list['country'];?>                         </option>
                 <?php
                }
             ?>
        </select></td>
 </tr>
<tr>
<td>State</td>
<td> :</td>
 <td> <select name="state" id="state" >
         <option value="">--- Select ---</option>
            <?php
             if (isset ($db1)&&$db1!=""){

                }
             ?>
            <?php
                 $list=mysql_query("select * from delivery group by state  ");
                    while($row_list=mysql_fetch_assoc($list)){
            ?>
                    <option value="<?php echo $row_list['state']; ?>"<?php  if($row_list['state']==$db1){echo "selected"; } ?>>
                                 <?php echo $row_list['state'];?>                           </option>
             <?php
                }
             ?>
        </select></td>

</tr>
<tr>
<td>City</td>
<td> :</td>
<td> <select name="city2" id="city2" >

        </select></td>
</tr>

</table>
</fieldset>

</form>
</body>
</html> 

ajax-city.php页面

<?php

include('db_connection.php');

$id=$_GET['state'];
$sql=mysql_query("select * from delivery where state='$id' ");
while($row=mysql_fetch_array($sql))
{
echo "<option value='$row[city]'>$row[city]</option>";
}


?>

0 个答案:

没有答案