好的,我正在创建一个电子商务网站,直到最近,update_cart(数量)代码一直正常运行。现在突然它决定提交我的表单而不是运行更新。我已经尝试过更改以及将其移到表单之外,希望能解决问题。我很遗憾我的代码很邋。如果您有任何想法或建议,请告诉我。我非常了解我的php,sql和html,但我对ajax和jquery知之甚少。
我花了最近6个小时的时间在这上面,所以我们非常感谢所有的帮助!
我的购物车
<form class="form" action="cart.php?checkout=1" method="post">
<table id="totals" class="table table-bordered table-condensed table-striped table-auto">
<thead>
<th>#</th>
<th>Item</th>
<th>Price</th>
<th>Quantity</th>
<th>Sub Total</th>
</thead>
<tbody>
<?php
foreach ($items as $item) {
$product_id = $item['id'];
$productQ = $db->query("SELECT * FROM products WHERE id = '{$product_id}'");
$product = mysqli_fetch_assoc($productQ);
?>
<tr>
<td><?=$i;?></td>
<td><?=$product['title'];?></td>
<input type="hidden" name="item<?=$i;?>" value="<?=$product['title'];?>">
<td><?=money($product['price']);?></td>
<input type="hidden" name="price<?=$i;?>" value="<?=$product['price'];?>">
<td>
<button class="btn btn-xs btn-default" onclick="update_cart('removeone','<?=$product['id'];?>');">-</button>
<?=$item['quantity'];?>
<?php if($item['quantity'] < $product['quantity']): ?>
<button class="btn btn-xs btn-default" onclick="update_cart('addone','<?=$product['id'];?>');">+</button>
<?php else: ?>
<span class="text-danger">Max</span>
<?php endif; ?>
</td>
<td ><?=money($item['quantity'] * $product['price']);?></td>
<input type="hidden" name="quantity<?=$i;?>" value="<?=$item['quantity'];?>">
</tr>
<?php
$i++;
$item_count += $item['quantity'];
$grand_total += ($product['price'] * $item['quantity']);
}
?>
</tbody>
</table>
<table id="totals" class="table table-bordered table-condensed text-right table-auto">
<thead class="totals-table-header">
<th></th>
<th></th>
<th></th>
</thead>
<tbody>
<tr>
<td>Total Items:</td>
<input type="hidden" name="rows" value="<?=$i;?>">
<td><?=$item_count;?></td>
<input type="hidden" name="item_count" value="<?=$item_count;?>">
</tr>
<tr>
<td>Total:</td>
<td class="bg-success"><?=money($grand_total);?></td>
<input type="hidden" name="total" value="<?=$grand_total;?>">
</tr>
<tr>
<td></td>
<td> <button type="submit" class="btn btn-primary btn-sm ">
<span class="glyphicon glyphicon-shopping-cart"></span>Check Out >>
</button></td>
</tr>
</tbody>
</table>
</div>
</form>
我的更新ajax(在页脚中)
function update_cart(mode,edit_id){
var data = {"mode" : mode, "edit_id" : edit_id};
jQuery.ajax({
url : '/admin/parsers/update_cart.php',
method : "post",
data : data,
success : function(){location.reload(true);},
error : function(){alert("Something went wrong.");},
});
}
最后是我的update_cart函数
<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/system/init.php';
$mode = sanitize($_POST['mode']);
$edit_id = sanitize($_POST['edit_id']);
$cartQ = $db->query("SELECT * FROM cart WHERE id = '{$cart_id}'");
$result = mysqli_fetch_assoc($cartQ);
$items = json_decode($result['items'], true);
$updated_items = array();
$domain =(($_SERVER['HTTP_HOST'] != 'localhost')?'.'.$_SERVER['HTTP_HOST']:false);
if ($mode == 'removeone') {
foreach ($items as $item) {
if ($item['id'] == $edit_id) {
$item['quantity'] = $item['quantity'] - 1;
}
if ($item['quantity'] > 0) {
$updated_items[] = $item;
}
}
}
if ($mode == 'addone') {
foreach ($items as $item) {
if ($item['id'] == $edit_id) {
$item['quantity'] = $item['quantity'] + 1;
}
$updated_items[] = $item;
}
}
if (!empty($updated_items)) {
$json_updated = json_encode($updated_items);
$db->query("UPDATE cart SET items = '{$json_updated}' WHERE id = '{$cart_id}'");
$db->query("UPDATE transactions SET status ='' WHERE cart_id = {$cart_id}");
$_SESSION['success_flash'] = 'Your shopping cart has been updated!';
}
if (empty($updated_items)) {
$db->query("DELETE FROM cart WHERE id = '{$cart_id}'");
setcookie(CART_COOKIE,'',1,"/",$domain,false);
}
?>
答案 0 :(得分:0)
button
的 type
会提交表单。您可以选择添加type="button"
或取消onclick
事件。