简体   繁体   中英

Auto-update total price in shopping cart automatically with ajax

Please help, Im new in php .. I'd like to update the shopping cart total by ajax when i change the quantity of any item ..

  <input type="text" name="qty" size="5" value="<?php if(isset($_SESSION['qty'])){ echo $_SESSION['qty']; } ?>" /> //This is the code of php <?php if(isset($_POST['updat_cart'])){ $qty = $_POST['qty']; $update_qty = "UPDATE cart SET qty = '$qty' "; $run_qty = mysqli_query($conn, $update_qty); $_SESSION['qty'] = $qty; $total = $total* $qty; } ?> 

AJAX requires more than just the php code. I suggest you to learn the semantics of it before going to implement. Here is a very good example for that. Now if you know the concept which is not evident from the code you posted, the above example just needs to be tweaked to get what you are trying to do, instead of SELECT * FROM user WHERE id = '".$q."' you need to have SELECT price FROM cart WHERE id = '".$q."' and qty = '$qty' after you change html page in the way you need. Additional reading:

  1. http://webdesign.tutsplus.com/tutorials/building-a-bootstrap-contact-form-using-php-and-ajax--cms-23068

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM