Good day!

There is such a code for adding goods to the cart, somehow you can display a notification when you click on add to cart. I tried to set the condition for the button, it does not work, but only 2 buttons are displayed - add to the basket, one leads to the successful addition to the basket, and the other leads to the output of this notification.

<?php if($_SESSION['total_quantity']): ?> <a class="clickMe" href="?view=addtocart&goods_id=<?=$eyestopper['goods_id']?>"><img class="addtocard-index" src="<?=TEMPLATE?>images/addcard-index.jpg" alt="Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ Π² ΠΊΠΎΡ€Π·ΠΈΠ½Ρƒ" /></a> <div class="overlay" title="ΠΎΠΊΠ½ΠΎ"></div> <div class="popup"> <div class="close_order">x</div> <p>Π’ΠΎΠ²Π°Ρ€ ΡƒΡΠΏΠ΅ΡˆΠ½ΠΎ Π΄ΠΎΠ±Π°Π²Π»Π΅Π½ Π² ΠΊΠΎΡ€Π·ΠΈΠ½Ρƒ</p> </div> <?php endif; ?> 

I provide below code:

 session_start(); /* ===Π‘ΡƒΠΌΠΌΠ° Π·Π°ΠΊΠ°Π·Π° Π² ΠΊΠΎΡ€Π·ΠΈΠ½Π΅ + Π°Ρ‚Ρ€ΠΈΠ±ΡƒΡ‚Ρ‹ Ρ‚ΠΎΠ²Π°Ρ€Π°===*/ function total_sum($goods, $connection){ $total_sum = 0; $str_goods = implode(',',array_keys($goods)); $query = "SELECT goods_id, name, price FROM goods WHERE goods_id IN ($str_goods)"; $res = mysqli_query($connection, $query) or die(mysqli_error()); while($row = mysqli_fetch_assoc($res)){ $_SESSION['cart'][$row['goods_id']]['name'] = $row['name']; $_SESSION['cart'][$row['goods_id']]['price'] = $row['price']; $total_sum += $_SESSION['cart'][$row['goods_id']]['qty'] * $row['price']; } return $total_sum; } /* ===Π‘ΡƒΠΌΠΌΠ° Π·Π°ΠΊΠ°Π·Π° Π² ΠΊΠΎΡ€Π·ΠΈΠ½Π΅ + Π°Ρ‚Ρ€ΠΈΠ±ΡƒΡ‚Ρ‹ Ρ‚ΠΎΠ²Π°Ρ€Π°===*/ case('addtocart'): // Π΄ΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅ Π² ΠΊΠΎΡ€Π·ΠΈΠ½Ρƒ $goods_id = abs((int)$_GET['goods_id']); addtocart($goods_id); $_SESSION['total_sum'] = total_sum($_SESSION['cart'], $connection); // ΠΊΠΎΠ»-Π²ΠΎ Ρ‚ΠΎΠ²Π°Ρ€Π° Π² ΠΊΠΎΡ€Π·ΠΈΠ½Π΅ + Π·Π°Ρ‰ΠΈΡ‚Π° ΠΎΡ‚ Π²Π²ΠΎΠ΄Π° Π½Π΅ΡΡƒΡ‰Π΅ΡΡ‚Π²ΡƒΡŽΡ‰Π΅Π³ΠΎ ID Ρ‚ΠΎΠ²Π°Ρ€Π° $_SESSION['total_quantity'] = 0; foreach($_SESSION['cart'] as $key => $value){ if(isset($value['price'])){ // Ссли ΠΏΠΎΠ»ΡƒΡ‡Π΅Π½Π° Ρ†Π΅Π½Π° Ρ‚ΠΎΠ²Π°Ρ€Π° ΠΈΠ· Π‘Π” - суммируСм ΠΊΠΎΠ»-Π²ΠΎ $_SESSION['total_quantity'] += $value['qty']; }else{ // ΠΈΠ½Π°Ρ‡Π΅ - удаляСм Ρ‚Π°ΠΊΠΎΠΉ ID ΠΈΠ· сСссиии (ΠΊΠΎΡ€Π·ΠΈΠ½Ρ‹) unset($_SESSION['cart'][$key]); } } redirect(); break; <h2><span>ΠšΠΎΡ€Π·ΠΈΠ½Π°</span></h2> <div> <p class="onK"> ?php if($_SESSION['total_quantity']): ?> Π’ΠΎΠ²Π°Ρ€ΠΎΠ² Π² ΠΊΠΎΡ€Π·ΠΈΠ½Π΅:<br /> <span><?=$_SESSION['total_quantity']?></span> Π½Π° сумму <span><?=$_SESSION['total_sum']?></span> Ρ€ΡƒΠ±. <img src="<?=TEMPLATE?>images/korzinaON.png" class="korzinaON" /> <a href="#"><img src="<?=TEMPLATE?>images/oformit.png" class="oformit" alt="ΠžΡ„ΠΎΡ€ΠΌΠΈΡ‚ΡŒ Π·Π°ΠΊΠ°Π·" /></a> <?php else: ?> <span class="offK">ΠšΠΎΡ€Π·ΠΈΠ½Π° пуста</span> <img src="<?=TEMPLATE?>images/korzinaOFF.png" class="korzinaOFF" /> <?php endif; ?> </p> </div> 

And connected:

 <script type="text/javascript" src="js/jquery.cookie.js"></script> <script type="text/javascript" src="js/workscripts.js"></script> <script type="text/javascript"> var query = '<?=$_SERVER['QUERY_STRING']?>';</script> 

Self notification:

  <a class="clickMe"href="#">ΠŸΡ€ΠΈ ΠΊΠ»ΠΈΠΊΠ΅ Π²Ρ‹ΡˆΠ»ΠΎ ΡƒΠ²Π΅Π΄ΠΎΠΌΠ»Π΅Π½ΠΈΠ΅</a> 

Function:

 var popup = $('.popup'), overlay = $('.overlay'), timer; overlay.add($('.close_order',popup)).click(hidePopup); $('a.clickMe').click(function (e){ e.preventDefault(); popup.add(overlay).finish().fadeIn(function(){ clearTimeout(timer); timer = setTimeout(hidePopup, 3000); }); }); function hidePopup(){ return popup.add(overlay).fadeOut(); } 

jQuery.cookie:

 jQuery.cookie = function(name, value, options) { if (typeof value != 'undefined') { // name and value given, set cookie options = options || {}; if (value === null) { value = ''; options.expires = -1; } var expires = ''; if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) { var date; if (typeof options.expires == 'number') { date = new Date(); date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000)); } else { date = options.expires; } expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE } // CAUTION: Needed to parenthesize options.path and options.domain // in the following expressions, otherwise they evaluate to undefined // in the packed version for some reason... var path = options.path ? '; path=' + (options.path) : ''; var domain = options.domain ? '; domain=' + (options.domain) : ''; var secure = options.secure ? '; secure' : ''; document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join(''); } else { // only name given, get cookie var cookieValue = null; if (document.cookie && document.cookie != '') { var cookies = document.cookie.split(';'); for (var i = 0; i < cookies.length; i++) { var cookie = jQuery.trim(cookies[i]); // Does this cookie string begin with the name we want? if (cookie.substring(0, name.length + 1) == (name + '=')) { cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); break; } } } return cookieValue; } }; 

And how to remove it when updating?

  • one
    @DimasikYES, some kind of complete porridge, nothing is clear. What is the condition for the button and what other two buttons appear? You add to the basket, it seems, in a simple way ( by reloading the page ), but JS doesn't know anything about the addition, which means it won't display the notification. - Deonis
  • @Deonis, I agree that I don’t know, I’m sitting, I think, it’s not working for now (how can he know about it? - DimasikYES September
  • @Deonis, I do not understand, that is, I do not need a notification with a message? as: in the same JS, output the message: "All is good!" and 2 point did not understand especially .. - "In JS, without rebooting, add". Sorry for not understanding ( - DimasikYES
  • @Deonis, the fact is that I took it ready and screwed it up, and the same for adding the product to the basket (code). Therefore, I do not know how to write it down. Well, I will attach the above code from the jquery-cookie, if there is time, please show. I will be very, very grateful for your help. Thank you so much for helping. It doesn't work (Maybe I am doing something wrong, please show me better :) - DimasikYES
  • @DimasikYES, I do not need the jquery-cookie code, I am well acquainted with it. But with what is not familiar, so it is with the scripts that you took somewhere. I see the output: leave everything as it is, after adding the goods ( on the server, as you have ), display the block with the message, and check in JS - if such a block is on the page, then hide it after 3 seconds or by clicking on the corresponding button. There is no need to catch the event of clicking on the button (!), But simply to see if there is a message box or not. - Deonis

0