唯一的第一个数量是更新,当我单击第二个更新按钮时,它也提供与第一个更新按钮相同的update_id,但在检查中它具有不同的ID。 我对Ajax有所了解,请提出一种简单的方法。
cart.php
这是购物车形式,我向action.php发送请求,它从actions.php带来数据,但是只有第一个数量在起作用,一个购物车正在更新,数据库也已更新,但只有第一个没有选择第二个更新按钮显示的更新ID与第一个按钮相同,我认为其中有一个小问题。
<!-- Shoping Cart -->
<div id="products">
<div class="m-l-25 m-r--38 m-lr-0-xl" id="cart_msg">
<!--Cart Message-->
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$('.overlay').show();
$.ajax({
url : "actions.php",
method : "POST",
data : {checkOutDetails:1},
success : function(data){
$('.overlay').hide();
$("#products").html(data);
}
})
// =================================================
$("body").delegate(".update","click",function(event){
var update = $(this).parent().parent().parent();
var update_id = update.find(".update").attr("update_id");
var qty = update.find(".qty").val();
$.ajax({
url : "actions.php",
method : "POST",
data : {updateCartItem:1,update_id:update_id,qty:qty},
success : function(data){
$("#cart_msg").html(data);
}
})
})
})
</script>
actions.php
<?php
include 'include/db.php';
include 'cssFiles.php';
function getRealIpAddr()
{
if ( !empty( $_SERVER['HTTP_CLIENT_IP'] ) )
{
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
elseif( !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) )
//to check ip passed from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
if (isset($_POST["checkOutDetails"])) {
echo'<form class="bg0 p-t-75 p-b-85" >
<div class="container">
<div class="row">
<div class="col-lg-10 col-xl-7 m-lr-auto m-b-50">
<div class="m-l-25 m-r--38 m-lr-0-xl">
<div class="wrap-table-shopping-cart">
<table class="table-shopping-cart" >
<tr class="table_head">
<th class="column-1">Product</th>
<th class="column-2"></th>
<th class="column-3">Price</th>
<th class="column-4">Quantity</th>
<th class="column-5">Total</th>
<th colspan="2" style="text-align: center;" class="column-6">Action</th>
</tr>
';
$ip_add= getRealIpAddr();
$total=0;
$ip_add_query="select * from cart where ip_add='$ip_add'";
$result=mysqli_query(
$conn,$ip_add_query);
$temp="";
while ($ip_rows=mysqli_fetch_array($result)) {
$pro_id=$ip_rows['id'];
$qty=$ip_rows['qty'];
$get_id_query="select * from product where id='$pro_id'";
$run_query=mysqli_query(
$conn,$get_id_query);
while ($p_info=mysqli_fetch_array($run_query)) {
$pro_price=array($p_info['price']);
$p_info['name'];
$p_info['img1'];
$p_info['price'];
$p_id=$p_info['id'];
$sum_price=array_sum($pro_price);
$total +=$sum_price;
echo'
<tr class="table_row">
<td class="column-1">
<input type="hidden" name="product_id[]" value="'.$p_id.'"/>
<input type="hidden" name="" value="'.$pro_id.'"/>
<div class="how-itemcart1">
<img src="admin/img/'. $p_info['img1'].'" alt="IMG">
</div>
</td>
<td class="column-2">'. $p_info['name'].'</td>
<td class="column-3">"$"'.$p_info['price'].'</td>
<td class="column-4">
<div class="wrap-num-product flex-w m-l-auto m-r-0">
<div class="btn-num-product-down cl8 hov-btn3 trans-04 flex-c-m">
<i class="fs-16 zmdi zmdi-minus"></i>
</div>
<input class="mtext-104 cl3 txt-center num-product qty" type="number" value="'.$qty.'">
<div class="btn-num-product-up cl8 hov-btn3 trans-04 flex-c-m">
<i class="fs-16 zmdi zmdi-plus"></i>
</div>
</div>
</td>
<td class="column-5">$ 36.00</td>
<td class="column-6">
<a href="#" update_id="'.$pro_id.'" class="btn btn-primary btn-sm update">Update</a>
</td>
</tr>';
}}
echo'</table>
</div>
<div class="flex-w flex-sb-m bor15 p-t-18 p-b-15 p-lr-40 p-lr-15-sm">
<div class="flex-w flex-m m-r-20 m-tb-5">
<input class="stext-104 cl2 plh4 size-117 bor13 p-lr-20 m-r-10 m-tb-5" type="text" name="coupon" placeholder="Coupon Code">
<div class="flex-c-m stext-101 cl2 size-118 bg8 bor13 hov-btn3 p-lr-15 trans-04 pointer m-tb-5">
Apply coupon
</div>
</div>
<!-- <button class="flex-c-m stext-101 cl2 size-119 bg8 bor13 hov-btn3 p-lr-15 trans-04 pointer m-tb-10" type="submit" name="submit">
Update Cart
</button> -->
</div>
</div>
</div>
<div class="col-sm-10 col-lg-7 col-xl-5 m-lr-auto m-b-50">
<div class="bor10 p-lr-40 p-t-30 p-b-40 m-l-63 m-r-40 m-lr-0-xl p-lr-15-sm">
<h4 class="mtext-109 cl2 p-b-30">
Cart Totals
</h4>
<div class="flex-w flex-t bor12 p-b-13">
<div class="size-208">
<span class="stext-110 cl2">
Subtotal:
</span>
</div>
<div class="size-209">
<span class="mtext-110 cl2">
$79.65
</span>
</div>
</div>
<div class="flex-w flex-t bor12 p-t-15 p-b-30">
<div class="size-208 w-full-ssm">
<span class="stext-110 cl2">
Shipping:
</span>
</div>
<div class="size-209 p-r-18 p-r-0-sm w-full-ssm">
<p class="stext-111 cl6 p-t-2">
There are no shipping methods available. Please double check your address, or contact us if you need any help.
</p>
<div class="p-t-15">
<span class="stext-112 cl8">
Calculate Shipping
</span>
<div class="rs1-select2 rs2-select2 bor8 bg0 m-b-12 m-t-9">
<select class="js-select2" name="time">
<option>Select a country...</option>
<option>USA</option>
<option>UK</option>
</select>
<div class="dropDownSelect2"></div>
</div>
<div class="bor8 bg0 m-b-12">
<input class="stext-111 cl8 plh3 size-111 p-lr-15" type="text" name="state" placeholder="State / country">
</div>
<div class="bor8 bg0 m-b-22">
<input class="stext-111 cl8 plh3 size-111 p-lr-15" type="text" name="postcode" placeholder="Postcode / Zip">
</div>
<div class="flex-w">
<div class="flex-c-m stext-101 cl2 size-115 bg8 bor13 hov-btn3 p-lr-15 trans-04 pointer">
Update Totals
</div>
</div>
</div>
</div>
</div>
<div class="flex-w flex-t p-t-27 p-b-33">
<div class="size-208">
<span class="mtext-101 cl2">
Total:
</span>
</div>
<div class="size-209 p-t-1">
<span class="mtext-110 cl2">
"$".'.$total.'
</span>
</div>
</div>
<button class="flex-c-m stext-101 cl0 size-116 bg3 bor14 hov-btn3 p-lr-15 trans-04 pointer">
Proceed to Checkout
</button>
</div>
</div>
</div>
</div>
</form>';
}
if (isset($_POST["updateCartItem"])) {
$ip_add= getRealIpAddr();
$update_id = $_POST["update_id"];
$qty = $_POST["qty"];
$sql = "UPDATE cart SET qty='$qty' WHERE id='$update_id' AND ip_add = '$ip_add'";
if(mysqli_query($conn,$sql)){
echo "<div class='alert alert-info'>
<a href='#' class='close' data-dismiss='alert' aria-label='close'>×</a>
<b>Product is updated</b>
</div>";
exit();
}
}
?>
答案 0 :(得分:0)
var update = $(this).parent()。parent()。parent();
看起来您在这里上升了太多?更新链接的父级是td
,父级是tr
,父级是完整的table
。
因此update.find(".update").attr(...)
仅适用于在整个表格中找到的第一个.update
元素。
答案 1 :(得分:0)
cart.php
<input class="mtext-104 cl3 txt-center num-product qty" type="number" id="qty'.$pro_id.'" value="'.$qty.'">
<a href="#" onclick="test('.$pro_id.')" class="btn btn-primary btn-sm update">Update</a>
<script type="text/javascript">
function test(data){
var temp="#qty"+data;
var Qty=$(temp).val();
$.ajax({
url : "actions.php",
method : "POST",
data : {update:1,qty:Qty,update_id:data},
success : function(data){
}
})
}
</script>
actions.php
if (isset($_POST["update"])) {
$ip_add= getRealIpAddr();
$update_id = $_POST["update_id"];
$qty = $_POST["qty"];
$sql = "UPDATE cart SET qty='$qty' WHERE id='$update_id' AND ip_add = '$ip_add'";
if(mysqli_query($conn,$sql)){
echo "<div class='alert alert-info'>
<a href='#' class='close' data-dismiss='alert' aria-label='close'>×</a>
<b>Product is updated</b>
</div>";
}
}