簡體   English   中英

如何使用PHP,AJAX和JS復選框過濾類似產品?

[英]How do I filter for similar products using checkboxes with PHP, AJAX and JS?

這是我要完成的工作的圖片- 在此處輸入圖片說明

我的數據庫表示例- 在此處輸入圖片說明

我的目標是在不刷新頁面的情況下獲得與當前顯示的產品相似的產品。 我試圖通過使用復選框來找到類似的產品。

我首先使用$ _GET ['id']獲得了ID,該ID應該等於表中的值之一。

然后,我使用PDO Fetch獲取該特定ID的產品名稱,品牌,數量和價格,並將其存儲為字符串。

我需要幫助的是使用JQuery / AJAX獲取選中的復選框,然后將信息發送到PHP文件,該文件將檢查過濾器結果是否與表中的任何數據匹配。

我怎樣才能做到這一點?

這是我的product.php文件

 <?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>

我設法解決了這個問題,很高興我沒有放棄。

我的product.php文件

<?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>

這是我的JS文件

$(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();
});
});

這是我的PHP文件

<?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;
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM