За последние 24 часа нас посетили 17503 программиста и 1741 робот. Сейчас ищут 1111 программистов ...

Пустой responseText, метод POST

Тема в разделе "PHP для новичков", создана пользователем Dimon2x, 11 дек 2017.

  1. Dimon2x

    Dimon2x Старожил

    С нами с:
    26 фев 2012
    Сообщения:
    2.211
    Симпатии:
    186
    Не понимаю, почему responseText ничего не возвращает?

    Код (Javascript):
    1. <script>
    2.             var addCart = document.querySelectorAll('.add-to-cart');
    3.             var cart = document.querySelector('.count-cart');
    4.            
    5.        
    6.             addCart.forEach(function(element, i){
    7.                 addCart[i].addEventListener('click', function(e) {
    8.                     e.preventDefault();
    9.                     var id = addCart[i].dataset.id;
    10.  
    11.                     xmlhttp=new XMLHttpRequest();
    12.                            
    13.                     xmlhttp.onreadystatechange=function() {
    14.                         if (this.readyState==4 && this.status==200) {
    15.                             console.log(this.responseText);
    16.                             cart.innerHTML=this.responseText;
    17.                         }
    18.                     }
    19.                    
    20.  
    21.                     xmlhttp.open("POST", "/cart/addAjax/" + id, true);
    22.                     xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
    23.                     xmlhttp.send();
    24.                     return false;
    25.                 })
    26.             })
    27.            
    28.            
    29.         </script>
    HTML:
    1.  <li><a href="#"><i class="fa fa-shopping-cart"></i> Корзина <span class="count-cart">(<?php echo Cart::countItems()?>)</span></a></li>
    PHP:
    1. <?php
    2.     class Cart {
    3.         public static function addProduct($id) {
    4.             $id = intval($id);
    5.            
    6.             $productsInCart = [];
    7.            
    8.             if(isset($_SESSION['products'])) {
    9.                 $productsInCart = $_SESSION['products'];
    10.             }
    11.            
    12.             if(array_key_exists($id, $productsInCart)) {
    13.                 $productsInCart[$id]++;
    14.             }
    15.             else {
    16.                 $productsInCart[$id] = 1;
    17.             }
    18.            
    19.             $_SESSION['products'] = $productsInCart;
    20.            
    21.             return self::countItems();
    22.  
    23.         }
    24.        
    25.         public static function countItems() {
    26.             if(isset($_SESSION['products'])) {
    27.                 $count = 0;
    28.                 foreach($_SESSION['products'] as $id => $quantily) {
    29.                     $count = $count + $quantily;
    30.                 }
    31.                
    32.                 return $count;
    33.             }
    34.             else {
    35.                 return 0;
    36.             }
    37.         }
    38.     }
    В сессии счётчик увеличивается, думаю где-то проблема в js
    --- Добавлено ---
    HTML:
    1. <!-- это для синхронных<a href="/cart/add/<?php echo $product['id'];?>" class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart"></i>В корзину</a>-->
    2.                                                 <a href="#" data-id="<?php echo $product['id'];?>"
    3.                                                     class="btn btn-default add-to-cart">
    4.                                                     <i class="fa fa-shopping-cart"></i>В корзину
    5.                                                 </a>
    --- Добавлено ---
    А во вкладке Network, эти адишники отображаются
     
  2. Dimon2x

    Dimon2x Старожил

    С нами с:
    26 фев 2012
    Сообщения:
    2.211
    Симпатии:
    186
    Я разобрался

    PHP:
    1. return self::countItems();
    Заменил на
    PHP:
    1. echo self::countItems();