資料庫結構:
準備3個檔案:
1.cart.php // 最上層顯示檔案
2.cart_ajax.php // ajax處理資料
3.config.php //資料庫配置
一、cart.php
<pre name="code" class="html"><?phpinclude 'config.php';$sql = "select * from cart";$result = mysql_query($sql);$row = array();while($rows = mysql_fetch_array($result,MYSQL_ASSOC)){ $row[] = $rows;}//print_r($row);?><!DOCTYPE html><html lang="zh-hans"><head> <meta charset="UTF-8"> <title></title></head><body><table width="" border="1" cellspacing="0" cellpadding="0" align="center"> <tr> <td>商品名稱</td> <td>商品庫存</td> <td>商品單價</td> <td>購買數量</td> <td>小計</td> <td>操作</td> </tr> <!--遍曆資料--> <?php foreach($row as $key=>$val){?> <tr> <td><?php echo $val['name'] ?></td> <td><?php echo $val['total_quantity'] ?></td> <!--商品單價--> <td><input type="text" name="price" value="<?php echo $val['price'] ?>"></td> <td> <button onclick="minusCart(this, '<?php echo $val['id'] ?>')">-</button> <!--購買數量--> <input type="text" name="num" value="<?php echo $val['num'] ?>" max="<?php echo $val['total_quantity'] ?>" /> <button onclick="plusCart(this, '<?php echo $val['id'] ?>')">+</button> </td> <!--小計價格 --> <td><input type="text" name="subtotal_price" value="<?php echo $val['price']*$val['num'];?>" onclick="price()"></td> <td><button>編輯</button><button>刪除</button></td> </tr> <?php }?> <tr> <!--總價--> <td>總價</td> <td colspan="4">0元</td> </tr></table><!--<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.1.min.js"></script>--><script src="jquery-2.1.1.min.js"></script><script> function setPrice(o) {//設定小計和總價 var tr = o.closest('tr'); var ipt = tr.find('input'); ipt.filter(':last').val(parseInt(o.val()) * parseInt(ipt.eq(0).val(), 10)); var sum = 0; o.closest('tbody').find('input[name="subtotal_price"]').each(function () { sum += parseInt(this.value, 0) || 0; }) .end().find('td:last').html(sum+'元') } //減 function minusCart(_this, id){ var num_input = $(_this).next('input[name="num"]'); var num = parseInt(num_input.val()); num--; if(num <= 0){ return false; } else { num_input.val(num); setPrice(num_input); cartNum(num_input, id, num); } } //加 function plusCart(_this,id){ //擷取購買數量 var num_input = $(_this).prev('input[name="num"]'); var num = parseInt(num_input.val()); var total_quantity = parseInt(num_input.attr('max')); if(num >= total_quantity){ alert('庫存不足'); return false; }else { //alert(num); num = parseInt(num) + 1; num_input.val(num); setPrice(num_input); cartNum(num_input, id, num); } } /** * 修改購物車商品數量 * @param _this * @param id * @param num */ function cartNum(_this, id, num){ $.ajax({ type: 'POST', url: 'cart_ajax.php', data: {id: id, num: num}, dataType: 'json', success: function (res) { if (res.status == 1) { _this.val(num); }else{ alert(res.info); } } }); }</script></body></html>
二、config.php
<?php/** *email:scenewood@163.com *name:鄭小木 */$server = 'localhost';$data = 'shopping';mysql_connect($server,'root','root');mysql_set_charset('utf8');mysql_select_db($data);
三、cart_ajax.php
<?php/** *email:scenewood@163.com *name:鄭小木 */include 'config.php';//接受cart.php的資料if ($_POST) { $id = $_POST['id']; $num = $_POST['num']; $retureInfo = array( 'status' => 0, 'info' => '修改商品數量失敗' ); $sql = "UPDATE `cart` SET num='{$num}' WHERE `id`={$id}"; mysql_query($sql); $row = mysql_affected_rows(); if ($row == 1) { $retureInfo['status'] = 1; $retureInfo['info'] = '修改商品數量成功'; } echo json_encode($retureInfo);}
本文講解了購物車功能實現 通過 php+MySQL+jQuery+Ajax,實現購物車功能,更多相關內容請關注php中文網。