0

这个 ps_search.php 页面包含笔记本电脑、包等类别的下拉列表……以及城市的下拉列表。如果用户想发布广告意味着他/她想在网站上销售东西,首先选择一个类别,如笔记本电脑、包或其他任何东西。然后从发布广告的地方选择城市。例如,如果用户发布了来自 city1 的笔记本电脑广告,而我们没有更多来自其他城市的笔记本电脑广告。然后选择笔记本电脑后,城市 1 应出现在下拉菜单中。我有两个文件 ps_search.php 和 get_city.php

//ps_search.php

<form name="search" method="post" action="<?php echo $base_url ?>search_rides.php?go">
    <table width="100%" border="0" cellpadding="4" cellspacing="0" id="tbsrch-engine">
      <tbody>
        <tr>
          <td height="61">
            <strong>Choose Category:</strong>
            </td>
          <td>
            <select name="category_id" id="category_id" style="width: 155px;" onChange="get_city(this.value,'<?php echo $base_url ?>dropdown/get_city.php')">
              <option value="0">Any</option>
              <?php $query = "SELECT  category_id as id, category_name as name FROM tbl_ps_category ";

    $result = mysql_query($query);

?>
              <?php while ( $row = mysql_fetch_array($result)) {?>
              <option value="<?php echo $row['id'].'-'.$row['name']; ?>"><?php echo $row['name']?></option>
              <?php }?>
              </select>
            </td>
        </tr>
       <tr>
<td height="40">
<strong>City:</strong>
</td>
<td>
<div id="models">
<select name="city_id" id="city_id">
<option value="0">Any</option>
</select>
</div>
</td>
</tr>
        <tr>
          <td height="32"/>
          <td>
            <input type="submit" name="button2" id="button2" value="Search an Ad" class="fbutton"/>
            </td>
          </tr>
        </tbody>
      </table>
    </form> 

//get_city.php

<?php
include('../Connections/photohive.php');

 $id = $_REQUEST['id'];
$explode = explode('-',$id);
$id = $explode[0];


 $sql = "SELECT city_id FROM ".$ps_prefix."product WHERE category_id=".$id;
$query = mysql_query($sql);
//exit;
?>

<select name="city_id" id="city_id" style="width:155px;">
      <option value="0">Any</option>
 <?php 
while ($row = mysql_fetch_array($query)){
     $q1= sprintf("Select cityname from tbl_city where city_id='%s'" , mysql_real_escape_string($row['city_id']), " ORDER BY cityoreder ASC");
    $r1= mysql_query($q1);
    while($row2= mysql_fetch_assoc($r1))
    {
    ?>
    <option value="<?php echo $row['city_id']?>"><?php echo $row2['cityname']?></option>
<?php    
}
}

?>
</select>

我也包含了js文件

<script src="<?php echo $base_url;?>js/jquery.ajaxq-0.0.1.js" type="text/javascript"></script>

请帮我....

4

1 回答 1

0

您的 javascript 应如下所示:

(function ($) {
    // When category select changes
    $('#category_id').change(function() {
        // make ajax query to get cities
        $.get('get_city.php', {id: $(this).val())
         .done(function(response) {
             // get the cities dropdown from response and insert into page
             $('#models').html(response);
         });
    });
})(jQuery);

我个人不喜欢在我的 ajax 调用中返回 HTML。我通常更喜欢返回一个 JSON 对象,然后解析出 HTML 客户端(通常使用 Handlebars)。我认为这样更干净。

于 2013-05-29T12:37:40.630 回答