Autor | Zpráva | ||
---|---|---|---|
juraj Profil |
#1 · Zasláno: 20. 4. 2017, 16:38:55
Zdravím
Ako mám upraviť kód v ajaxe,aby sa vedla počtu objednaných kusov zobrazovala aj celková suma v eurách? Zdrojové súbory aj s mysql tabulkou si môžete stiahúť na adrese tu dakujem za rady |
||
juraj Profil |
#2 · Zasláno: 22. 4. 2017, 14:26:11
Nikto,nevie poradiť?
|
||
nodo Profil |
#3 · Zasláno: 22. 4. 2017, 16:13:48
Nikto si nebude stahovať súbory, ja by som ti odporučil vložiť sem tú časť kódu kde máš problém...
|
||
juraj Profil |
nodo:
v index.php <script> $(document).ready(function(){ $(".form-item").submit(function(e){ var form_data = $(this).serialize(); var button_content = $(this).find('button[type=submit]'); button_content.html('Adding...'); //Loading button text $.ajax({ //make ajax request to cart_process.php url: "cart_process.php", type: "POST", dataType:"json", //expect json value from server data: form_data }).done(function(data){ //on Ajax success $("#cart-info").html(data.items); //total items in cart-info element button_content.html('Add to Cart'); //reset button text to original text alert("Item added to Cart!"); //alert user if($(".shopping-cart-box").css("display") == "block"){ //if cart box is still visible $(".cart-box").trigger( "click" ); //trigger click to update the cart box. } }) e.preventDefault(); }); //Show Items in Cart $( ".cart-box").click(function(e) { //when user clicks on cart box e.preventDefault(); $(".shopping-cart-box").fadeIn(); //display cart box $("#shopping-cart-results").html('<img src="images/ajax-loader.gif">'); //show loading image $("#shopping-cart-results" ).load( "cart_process.php", {"load_cart":"1"}); //Make ajax request using jQuery Load() & update results }); //Close Cart $( ".close-shopping-cart-box").click(function(e){ //user click on cart box close link e.preventDefault(); $(".shopping-cart-box").fadeOut(); //close cart-box }); //Remove items from cart $("#shopping-cart-results").on('click', 'a.remove-item', function(e) { e.preventDefault(); var pcode = $(this).attr("data-code"); //get product code $(this).parent().fadeOut(); //remove item element from box $.getJSON( "cart_process.php", {"remove_code":pcode} , function(data){ //get Item count from Server $("#cart-info").html(data.items); //update Item count in cart-info $(".cart-box").trigger( "click" ); //trigger click on cart-box to update the items list }); }); }); </script> </head> <body> <div align="center"> <h3>Ajax Shopping Cart Example</h3> </div> <a href="#" class="cart-box" id="cart-info" title="View Cart"> <?php if(isset($_SESSION["products"])){ echo count($_SESSION["products"]); echo "<br>"; echo €; }else{ echo 0; } ?> </a> <div class="shopping-cart-box"> <a href="#" class="close-shopping-cart-box" >Close</a> <h3>Your Shopping Cart</h3> <div id="shopping-cart-results"> </div> </div> <?php //List products from database $results = $mysqli_conn->query("SELECT product_name, product_desc, product_code, product_image, product_price FROM products_list"); //Display fetched records as you please $products_list = '<ul class="products-wrp">'; while($row = $results->fetch_assoc()) { $products_list .= <<<EOT <li> <form class="form-item"> <h4>{$row["product_name"]}</h4> <div><img src="images/{$row["product_image"]}"></div> <div>Price : {$currency} {$row["product_price"]}<div> <div class="item-box"> <div> Color : <select name="product_color"> <option value="Red">Red</option> <option value="Blue">Blue</option> <option value="Orange">Orange</option> </select> </div> <div> Qty : <select name="product_qty"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> </div> <div> Size : <select name="product_size"> <option value="M">M</option> <option value="XL">XL</option> <option value="XXL">XLL</option> </select> </div> <input name="product_code" type="hidden" value="{$row["product_code"]}"> <button type="submit">Add to Cart</button> </div> </form> </li> EOT; } $products_list .= '</ul></div>'; echo $products_list; ?> nodo: kód v súbore cart_process.php <?php session_start(); //start session include_once("config.inc.php"); //include config file setlocale(LC_MONETARY,"en_US"); // US national format (see : http://php.net/money_format) ############# add products to session ######################### if(isset($_POST["product_code"])) { foreach($_POST as $key => $value){ $new_product[$key] = filter_var($value, FILTER_SANITIZE_STRING); //create a new product array } //we need to get product name and price from database. $statement = $mysqli_conn->prepare("SELECT product_name, product_price FROM products_list WHERE product_code=? LIMIT 1"); $statement->bind_param('s', $new_product['product_code']); $statement->execute(); $statement->bind_result($product_name, $product_price); while($statement->fetch()){ $new_product["product_name"] = $product_name; //fetch product name from database $new_product["product_price"] = $product_price; //fetch product price from database if(isset($_SESSION["products"])){ //if session var already exist if(isset($_SESSION["products"][$new_product['product_code']])) //check item exist in products array { unset($_SESSION["products"][$new_product['product_code']]); //unset old item } } $_SESSION["products"][$new_product['product_code']] = $new_product; //update products with new item array } $total_items = count($_SESSION["products"]); //count total items die(json_encode(array('items'=>$total_items))); //output json } ################## list products in cart ################### if(isset($_POST["load_cart"]) && $_POST["load_cart"]==1) { if(isset($_SESSION["products"]) && count($_SESSION["products"])>0){ //if we have session variable $cart_box = '<ul class="cart-products-loaded">'; $total = 0; foreach($_SESSION["products"] as $product){ //loop though items and prepare html content //set variables to use them in HTML content below $product_name = $product["product_name"]; $product_price = $product["product_price"]; $product_code = $product["product_code"]; $product_qty = $product["product_qty"]; $product_color = $product["product_color"]; $product_size = $product["product_size"]; $cart_box .= "<li> $product_name (Qty : $product_qty | $product_color | $product_size ) — $currency ".sprintf("%01.2f", ($product_price * $product_qty)). " <a href=\"#\" class=\"remove-item\" data-code=\"$product_code\">×</a></li>"; $subtotal = ($product_price * $product_qty); $total = ($total + $subtotal); } $cart_box .= "</ul>"; $cart_box .= '<div class="cart-products-total">Total : '.$currency.sprintf("%01.2f",$total).' <u><a href="view_cart.php" title="Review Cart and Check-Out">Check-out</a></u></div>'; die($cart_box); //exit and output content }else{ die("Your Cart is empty"); //we have empty cart } } ################# remove item from shopping cart ################ if(isset($_GET["remove_code"]) && isset($_SESSION["products"])) { $product_code = filter_var($_GET["remove_code"], FILTER_SANITIZE_STRING); //get the product code to remove if(isset($_SESSION["products"][$product_code])) { unset($_SESSION["products"][$product_code]); } $total_items = count($_SESSION["products"]); die(json_encode(array('items'=>$total_items))); } ?> |
||
Tomášeek Profil |
#5 · Zasláno: 22. 4. 2017, 21:10:07
Který kilometr z těch několika mnoha zde nakopirovaneho kódu je problematicky?
Vyber z nej deset řádku, ktere problém způsobují. Nikdo debugovat za tebe nebude. |
||
Časová prodleva: 9 dní
|
|||
petr Profil * |
#6 · Zasláno: 1. 5. 2017, 21:56:02
Řešil bych to takhle:
cart_process.php <?php # … if(isset($_POST["product_code"])) { # … while($statement->fetch()){ # … $_SESSION["products"][$new_product['product_code']] = $new_product; //update products with new item array } $total_items = count($_SESSION["products"]); //count total items $total_price = array_sum($_SESSION["products"]); die(json_encode(array('items'=>$total_items,'total_price'=>$total_price))); //output json } # … <script> // … $.ajax({ //make ajax request to cart_process.php // … }).done(function(data){ //on Ajax success $("#cart-info").html("₹" + data.total_price + " " + data.items); //total items in cart-info element // … </script> |
||
Časová prodleva: 13 dní
|
|||
juraj Profil |
petr:
Nejako mi $total_price nechce ukazovať výslednú sumu
Dakujem za radu bude v inej mene |
||
Časová prodleva: 7 let
|
0