从php发送多个值到ajax

时间:2015-04-28 06:20:26

标签: php jquery ajax

我试图将页面不同部分的2个值发送到同一个ajax函数。基本上,它是一个搜索过滤器,其中选择类别是一个模块,而选择品牌是另一个,它们都是产品的属性并且彼此独立。

我需要3个条件来执行搜索

  1. 仅在选择品牌或
  2. 仅在选择了cat或
  3. 如果两者都被选中。
  4. 我的问题是1 and 3 are working。但2nd condition isn't因为它将猫的价值作为品牌。请帮我纠正我的功能。

    我的AJAX功能:

    function filter(subid) {
      alert(subid);
      var brand_filter = new Array();
        $("input:checked").each(function() {
        brand_filter.push($(this).val());
        });
    $.ajax({
        type: 'POST',
        url: 'getSubcategoryProducts.php',
        data: {
           brand_filter:brand_filter,
            subId: subid
        },
       success: function(data) {
        //alert(data);
        $('#productDetail').html(data).fadeIn('slow');
        $('#sub_cat_menuItem a').addClass('.collection-category-brands li.active, .collection-category-brands li:hover');
        }
    });
    

    }

    选择品牌的模块:

        <div class="collection-brands-filter">
                        <h4>Filter by Brands</h4>
                        <div class="brand-filter-form">
                            <form>
                                <?php $brandlist    =   $objBrands-> getAll("status=1");
                                foreach($brandlist as $branditem){
                                ?>
                                <span><input type="checkbox" name="brand_filter[]" <?php if ('checked="checked"'){ ?> onClick="filter(<?php echo $branditem['id'];?>);" <?php } ?> value="<?php echo $branditem['id'];?>"/><?php echo $branditem['name'];?></span>
                                <?php } ?>
                            </form>
                        </div>
                    </div>
    

    选择类别的模块:

        <div class="collection-category-brands">
                        <ul>
    
    
                            <?php 
                            //Fetching sub categories of the main_category
                            while($query2res    =   mysql_fetch_array($query2)){ ?>
                            <li id="sub_cat_menuItem"><a href="#" onClick="filter(<?php echo $query2res['id'];?>);"><?php echo $query2res['1'];?></a></li>
                            <?php $subId    =   $query2res['id'];}?>
    
                        </ul>
                    </div>
    

    getSubcategoryProducts.php代码:

        <?php 
        include_once("class/site_root.php");
        include('includes/connect.php');
        include_once(DIR_ROOT."class/functions.php");
        include_once(DIR_ROOT."class/products.php");
        $objProducts    =   new products();
    
         $subId =   $_POST['subId'];
    
        //IF ONLY BRAND IS SELECTED AND NOT SUB CATEGORY
       if(isset($_POST['brand_filter'])&&!isset($_POST['subId']))
       {
      echo "only brand is selected";
       foreach($_POST['brand_filter'] as $item)
    {
         $res   =   $objProducts->getAll('status=1 and brand_id='.$item);?>
        <ul>
        <?php foreach($res as $val){?>
    
        <li>
                <img src="<?php echo $val['img_small']; ?>" alt="icon"/>
                <div class="collection-item-hover">
                    <div class="collection-category-detail">
                        <a href="single-retail.php?id=<?php echo $val['id'];?>">Know More</a>
                        <div class="collection-item-detail">
                            <h4><?php echo $val['product_name']; ?></h4>
                           <p><?php echo $val['descr_small']; ?></p>
                        </div>
                    </div>
                </div>
            </li>
        <?php }?>
         </ul>
       <?php }
        }
    
        //IF ONLY SUB CATEGORY IS CHOSEN!
        else if(isset($subId)&&!isset($_POST['brand_filter'])){
        echo "only sub cat selected!";
        $productsquery=mysql_query("SELECT products.product_name,products.id,products.main_id,
                            products.img_small,products.descr_small,sub_category.name 
                            FROM products 
                            LEFT JOIN sub_category on
                            products.sub_id=sub_category.id 
                            WHERE sub_category.id=".$subId);
          $count    =   mysql_num_rows($productsquery);
    
          $subNameVal=mysql_fetch_array(mysql_query("select name from  sub_category where id=".$subId));
    
          ?>
         <h3><?php echo $subNameVal['name'];?></h3>
         <?php
        if($count>0)
        {
        ?>
      <ul>
        <?php 
      while($productsRes=mysql_fetch_array($productsquery))
          {
        ?>
             <li>
        <img src="<?php echo $productsRes['img_small']; ?>" alt="icon"/>
        <div class="collection-item-hover">
            <div class="collection-category-detail">
                <a href="single-retail.php?id=<?php echo $productsRes['id'];?>">Know More</a>
                <div class="collection-item-detail">
                    <h4><?php echo $productsRes['product_name']; ?></h4>
                   <p><?php echo $productsRes['descr_small']; ?></p>
                </div>
            </div>
        </div>
        </li>
    <?php }
     ?>
    </ul>
    <?php }
    
    }
    
    //IF BOTH ARE SELECTED
    else //if(isset($_POST['brand_filter'])&&isset($subId))
    {
    echo "both selected!";
    echo 
    
    "SUB-".$subId."<br/>";
              foreach ($_POST['brand_filter'] as $item)
               {
               echo "brand-".$item."<br/>";
               }
              }
            ?>
    

0 个答案:

没有答案