duanping5306 2015-07-25 15:05
浏览 47
已采纳

如何使用PHP,AJAX和JS复选框过滤类似产品?

Here is an image of what I'm trying to accomplish - enter image description here

Example of my Database Table - enter image description here

My goal is to get a similar product to the product that is currently displaying without refreshing the page. I am trying to find similar products is by using check boxes.

I first got the id using $_GET['id'] which should be equal to one of the values in my table.

I then used PDO Fetch to get the product name, brand, quanity and price of that particular id and store it as a string.

What I need help with is using JQuery/AJAX to get the checkboxes that are checked and then send the information to a PHP file that would check if filter results match with any data from the table.

How can I do this?

This is my product.php file

 <?php
    require ('includes/db.php');

    $id = $_GET['id']; //Getting the ID in URL. ex products.php?id=12

    $stmt = $handler->prepare("SELECT * FROM products WHERE id = '$id' ");
    $result = $stmt->execute(array());
    $products   = $stmt->fetch(PDO::FETCH_ASSOC);

    $prod_name = $products['prod_name']; //Product Name

    $brand = $products['brand']; //Product Brand
    $quantity = $products['quantity']; //Product Quantity
    $calories = $products['calories'];  //Product Calories
    $price = $products['price'];    //Product Price


?>

    <!DOCTYPE html>
     <html>
    <head>
        <title><?php echo "$brand $prod_name"; ?></title>
    </head>

    <body>
        <h1><?php echo $prod_name; ?></h1>
        <br />

        <p>Brand = <?php echo " $brand"; ?></p>
        <p>Quantity = <?php echo " $quantity"; ?></p>
        <p>Calories = <?php echo " $calories"; ?></p>
        <p>Price = <?php echo " $price"; ?></p>

        <br />

        <p style="text-align: center;">Find Similar Products</p>

        <form> 
        <div class="checkboxes">
            <label>
                <input name="brand" type="checkbox" value="<?php echo $brand; ?>">
                <span>Brand</span> <!--Brand Checkbox-->
            </label>
        </div>
        <div class="checkboxes">
            <label>
                <input name="quanitity" type="checkbox" value="<?php echo $quantity; ?>">
                <span>Quantity</span> <!--Quantity Checkbox-->
            </label>
        </div>
        <div class="checkboxes">
            <label>
                <input name="calories" type="checkbox" value="<?php echo $calories; ?>">
                <span>Calories</span> <!--Calories Checkbox-->
            </label>
        </div>
        <div class="checkboxes">
            <label>
                <input name="price" type="checkbox" value="<?php echo $price; ?>">
                <span>Price</span>  <!--Price Checkbox-->
            </label>
        </div>
    </form>

    </body>
</html>
  • 写回答

1条回答 默认 最新

  • doulian4467 2015-07-26 12:11
    关注

    I managed to solve this out, glad I didn't give up.

    My product.php File

    <?php
    require ('/db.php');
    
    $id = $_GET['id']; //Getting the ID in URL. ex products.php?id=12
    
    $stmt = $handler->prepare("SELECT * FROM products WHERE id = '$id' ");
    $result = $stmt->execute(array());
    $products   = $stmt->fetch(PDO::FETCH_ASSOC);
    
    $prod_name = $products['prod_name']; //Product Name
    
    $brand = $products['brand']; //Product Brand
    $quantity = $products['quantity']; //Product Quantity
    $calories = $products['calories'];  //Product Calories
    $price = $products['price'];    //Product Price
    
    
    ?>
    
    <!DOCTYPE html>
     <html>
    <head>
        <title><?php echo "$brand $prod_name"; ?></title>
    </head>
    
    <body>
        <h1><?php echo $prod_name; ?></h1>
        <br />
    
        <p>Brand = <?php echo " $brand"; ?></p>
        <p>Quantity = <?php echo " $quantity"; ?></p>
        <p>Calories = <?php echo " $calories"; ?></p>
        <p>Price = <?php echo " $price"; ?></p>
    
        <br />
    
        <p style="text-align: center;">Find Similar Products</p>
    
        <form method="post" action=""> 
        <div class="checkboxes">
            <label>
                <input name="brand" type="checkbox" value="<?php echo $brand; ?>">
                <span>Brand</span> <!--Brand Checkbox-->
            </label>
        </div>
        <div class="checkboxes">
            <label>
                <input name="quanitity" type="checkbox" value="<?php echo $quantity; ?>">
                <span>Quantity</span> <!--Quantity Checkbox-->
            </label>
        </div>
        <div class="checkboxes">
            <label>
                <input name="calories" type="checkbox" value="<?php echo $calories; ?>">
                <span>Calories</span> <!--Calories Checkbox-->
            </label>
        </div>
        <div class="checkboxes">
            <label>
                <input name="price" type="checkbox" value="<?php echo $price; ?>">
                <span>Price</span>  <!--Price Checkbox-->
            </label>
        </div>
    </form>
    <div class="filter_container">
    
        <div style="clear:both;"></div>
    </div>
    <script type="text/javascript" src="/js/filter.js"></script>
    
    <input name="prod_name" value="<?php echo $prod_name ?>" style="display:none;"/>
    <!--Hidden product name-->
    </body>
    </html>
    

    Here is my JS File

    $(document).ready(function() {
    function showValues() {
    
    
        var brand;  
        var quantity;   
        var calories;       
        var price;      
    
        //Gets product name
        var prod_name = $('input[name="prod_name"]').val(); 
    
        //Gets brand
        if($('input[name="brand"]').is(':checked')) 
            {brand = $('input[name="brand"]').val();} else {brand = ""}
    
        //Gets quantity
        if($('input[name="quantity"]').is(':checked')) 
            {quantity = $('input[name="quantity"]').val();} else {quantity = ""}
    
        //Gets calories
        if($('input[name="calories"]').is(':checked')) 
            {calories = $('input[name="calories"]').val();} else {calories = ""}
    
        //Gets price
        if($('input[name="price"]').is(':checked'))  
            {price = $('input[name="price"]').val();} else {price = ""}
    
    
            $.ajax({
            type: "POST",
            url: "/query.php",
            data: {'brand':brand, 'quantity':quantity, 'calories':calories, 'prod_name':prod_name}, 
            cache: false,
            success: function(data){
                $('.filter_container').html(data)
                    }
                });
    
    
    }
    
    //Call function when checkbox is clicked
    $("input[type='checkbox']").on( "click", showValues );
    
    //Remove checked when checkbox is checked
    $(".checkboxes").click(function(){
        $(this).removeAttr('checked');      
        showValues();
    });
    });
    

    Here is my PHP File

    <?php
    include('/db.php');
    
    $prod_name = $_POST['prod_name'];
    
    $Cbrand = $_POST['brand'];
    $Cquantity = $_POST['quantity'];
    $Ccalories = $_POST['calories'];
    $Cprice = $_POST['price'];
    
    
    if(!empty($Cbrand))
    {
        $data1 = "brand = '$Cbrand' AND";
    }else{
        $data1 = "";
    }
    if(!empty($Cquantity))
    {
        $data2 = "quantity = '$Cquantity' AND";
    }else{
        $data2 = "";
    }
    if(!empty($Ccalories))
    {
        $data3 = "calories = '$Ccalories' AND";
    }else{
        $data3 = "";
    }
    if(!empty($Cprice))
    {
        $data4 = "price = '$Cprice'";
    }else{
        $data4 = "";
    }
    
    
    $main_string = "WHERE $data1 $data2 $data3 $data4"; //All details
    
    $stringAnd = "AND"; //And
    
    $main_string = trim($main_string); //Remove whitespaces from the beginning and end of the main string
    
    $endAnd = substr($main_string, -3); //Gets the AND at the end
    
    if($stringAnd == $endAnd)
    {
    $main_string = substr($main_string, 0, -3);
    }else if($main_string == "WHERE"){
        $main_string = "";
    }
    else{
        $main_string = "WHERE $data1 $data2 $data3 $data4";
    }
    
    if($main_string == ""){ //Doesn't show all the products
    
    }else{
    
    $sql = "SELECT COUNT(*) FROM products $main_string";
    
    if ($res = $handler->query($sql)) {
    
    /* Check the number of rows that match the SELECT statement */
    if ($res->fetchColumn() > 0) {
      $sql = "SELECT * FROM products $main_string";
    foreach ($handler->query($sql) as $pro) {
    
    if(($pro['prod_name'] == $prod_name) && ($res->fetchColumn() < 2))
    {
        //The product currently being displayed is blank when using the filter
    }
    else{
        ?>
    
        <!------------------------------------------------------------------------------------------------------------------------------------------------->    
    
        <div class="form-result">
            <td><?=strtoupper($pro['brand']) + " " + strtoupper($pro['prod_name']); ?></td>
        </div>
    
        <!------------------------------------------------------------------------------------------------------------------------------------------------->
    
    
        <?php
    }
    }
    }    /* No rows matched -- do something else */
        else {
            ?>
            <div align="center"><h2 style="font-family:'Arial Black', Gadget, sans-serif;font-size:30px;color:#0099FF;">No Results with this filter</h2></div>
            <?php
            }
        }
    }
    $handler = null;
    $res = null;
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法