在城市中搜索所选类别

时间:2013-05-29 12:23:34

标签: php mysql

此ps_search.php页面包含笔记本电脑,行李等类别的下拉列表以及城市下拉列表。如果用户想要发布广告意味着他/她想要在网站上销售某些东西,首先选择类别如笔记本电脑,手提包或其他任何类别。然后从广告发布的位置选择城市。例如,如果用户从city1发布了笔记本电脑的广告,我们就没有其他城市的笔记本电脑广告。然后在选择笔记本电脑后,城市1应出现在下拉菜单中。我有两个文件ps_search.php ang 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>

请帮助我....

1 个答案:

答案 0 :(得分: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)。我认为它更清洁。