0
私はeコマースのウェブサイトを作りました。最近まで、update_cart(数量)コードはうまくいきました。今すぐ突然、更新プログラムを実行する代わりにフォームを送信することにしました。私は、問題を解決するためにフォームの外に移動するだけでなく、多くの変更を試みました。私は、あまりにもお粗末なコードには申し訳なく思っています。ご意見やご提案がありましたらお知らせください。私は私のPHP、SQL、およびHTMLをかなりよく知っていますが、私はajaxとjqueryに関してはほとんど分かりません。Ajaxの呼び出しが、想定されていない場合に送信されますか?
私はこの上に私の頭を打つ最後の6時間を費やしているので、すべての助けが大いに感謝! type
なし
マイカート
<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>
(フッターに)私の更新アヤックス
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);
}
?>