weixin_33721344 2016-05-19 20:08 采纳率: 0%
浏览 28

使用ajax过滤产品

I have an app in php where I have to filter some products by category using Ajax and I don't have any idea how.

My all php code:

<?php
session_start();
include_once("config.php");


//current URL of the Page. cart_update.php redirects back to this URL
$current_url = urlencode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Shopping Cart</title>
<link href="style/style.css" rel="stylesheet" type="text/css">
</head>
<body>

<h1 align="center">Products </h1>

<!-- Products List Start -->
<?php
$results = $mysqli->query("SELECT product_code, product_name, product_desc, price FROM products ORDER BY id ASC");
if($results){ 
$products_item = '<ul class="products">';
//fetch results set as object and output HTML
while($obj = $results->fetch_object())
{
$products_item .= <<<EOT

<form method="post" action="cart_update.php">
<table>
<tr>
<td> Name: {$obj->product_name}</td>
<td>Category: {$obj->product_desc}</td>
<td> Price: {$currency}{$obj->price} </td>
<td>
    <span>Color: </span>
    <select name="product_color">
    <option value="Black">Black</option>
    <option value="Silver">Silver</option>
    </select>
</td>
<td>
    <span>Quantity: </span>
    <input type="text" size="2" maxlength="2" name="product_qty" value="1" />
</td>
<td>
<div align="center"><button type="submit" class="add_to_cart">Add</button></div></td>
<input type="hidden" name="product_code" value="{$obj->product_code}" />
<input type="hidden" name="type" value="add" />
<input type="hidden" name="return_url" value="{$current_url}" />

</div></div>
</form>
</table>
EOT;
}
$products_item .= '</ul>';
echo $products_item;
}
?>    
<!-- Products List End -->

<?php
if(isset($_SESSION["cart_products"]) && count($_SESSION["cart_products"])>0)
{

echo '<h3><center>Your Shopping Cart</center></h3>';
echo '<form method="post" action="cart_update.php">';
echo '<table width="30%"  cellpadding="6" cellspacing="0"';
echo '<tbody>';

$total =0;
$b = 0;
foreach ($_SESSION["cart_products"] as $cart_itm)
{
    $product_name = $cart_itm["product_name"];
    $product_qty = $cart_itm["product_qty"];
    $product_price = $cart_itm["product_price"];
    $product_code = $cart_itm["product_code"];
    $product_color = $cart_itm["product_color"];
    $bg_color = ($b++%2==1) ? 'odd' : 'even'; //zebra stripe
    echo '<tr class="'.$bg_color.'">';
    echo '<td>Qty <input type="text" size="2" maxlength="2" name="product_qty['.$product_code.']" value="'.$product_qty.'" /></td>';
    echo '<td>'.$product_name.'</td>';
    echo '<td><input type="checkbox" name="remove_code[]" value="'.$product_code.'" /> Remove</td>';
    echo '</tr>';
    $subtotal = ($product_price * $product_qty);
    $total = ($total + $subtotal);
}
echo '<td colspan="4">';
echo '<button type="submit">Update</button>';
echo '</td>';
echo '</tbody>';
echo '</table>';
echo '</h1>';
$current_url = urlencode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
echo '</form>';
echo '</div>';


}
?>


<div id="maindiv">

 <select id="options">
  <option value="v1">Category</option>

</select>

<table id="destinations" border="1">
    <tr>
      <th>Product</th>
      <th>Category</th>
      <th>Price</th>
      <th>Color</th>
      <th>Quantity</th>
    </tr>

</table>



</div>


</body>
</html>

I really can not understand how to make this filter.Can someone gives me some ideas in order to resolve this task?

  • 写回答

1条回答 默认 最新

  • weixin_33721427 2016-05-19 20:24
    关注

    You haven't given us much to go on in terms of what this "filter" is. If I assume by "filter" you mean you wish to alter your SQL query according to some user-initiated AJAX call(s) to this script, then you'll need the following:

    • A POST or GET request sent via AJAX containing the database fields you wish to filter on. Note: Use some sort of alias or map instead of passing actual database column-names where the user can see it, that'd be a security flaw - also ensure you escape (clean-up) any user-input before it goes anywhere near your SQL queries :-)
    • A WHERE clause to insert into your SQL query, constructed dynamically from the above POST or GET data

    That's pretty much it.

    Very Rough example:

    $sql = "SELECT product_code, product_name, product_desc, price FROM products";
    // Where $_POST['filter'] comes from an AJAX POST request in the frontend
    if (!empty($_POST['filter'])) {
        $codeSql = ' ' . (!empty($_POST['code']) ? "product_code = '" . mysqli_escape_string($_POST['code']) . '" : '');
        $nameSql = ' ' . (!empty($_POST['name']) ? "product_name = '" . mysqli_escape_string($_POST['name']) . '" : '');
        $sql .= "WHERE " . $codeSql . $nameSql;
    }
    $sql .= " ORDER BY id ASC";
    
    $results = $mysqli->query($sql);
    
    评论

报告相同问题?