M
M
mutaev_murad2020-05-20 19:49:47
PHP
mutaev_murad, 2020-05-20 19:49:47

Handling ajax request?

I have some MVC and it has a file

Product.php

<?php 

  /**
   * 
   */
  class Product
  {
    const SHOW_BY_DEFAULT = 10;

    /**
     *
     */

    public static function getLatestProducts($count = self::SHOW_BY_DEFAULT)
    {
      $count = intval($count);

      $db = Db::getConnection();

      $productsList = array();

      $result = $db->query('SELECT id, name, price, image, is_new, data_id, description FROM product '
          . 'WHERE status = "1"'
          . 'ORDER BY id DESC '
          . 'LIMIT ' . $count);

      $i = 0;

      while ($row = $result->fetch()) {
        var_dump($productsList[$i]['id'] = $row['id']);
        var_dump($productsList[$i]['name'] = $row['name']);
        var_dump($productsList[$i]['image'] = $row['image']);
        var_dump($productsList[$i]['price'] = $row['price']);
        var_dump($productsList[$i]['is_new'] = $row['is_new']);
        var_dump($productsList[$i]['data_id'] = $row['data_id']);
        
        
        $productsList[$i]['description'] = $row['description'];

        $i++;
      }

      return $productsList;

    }
  /**
   * 
   */

  public static function getProductsListByCategory($categoryId = false, $page = 1)
    {
        if ($categoryId) {
            
            $page = intval($page);            
            $offset = ($page - 1) * self::SHOW_BY_DEFAULT;
        
            $db = Db::getConnection();            
            $products = array();
            $result = $db->query("SELECT id, name, price, image, is_new, description  FROM product "
                    . "WHERE status = '1' AND category_id = '$categoryId' "
                    . "ORDER BY id ASC "                
                    . "LIMIT ".self::SHOW_BY_DEFAULT
                    . ' OFFSET '. $offset);

            $i = 0;
            while ($row = $result->fetch()) {
                $products[$i]['id'] = $row['id'];
                $products[$i]['name'] = $row['name'];
                $products[$i]['image'] = $row['image'];
                $products[$i]['price'] = $row['price'];
                $products[$i]['is_new'] = $row['is_new'];
                $products[$i]['description'] = $row['description'];
                $i++;
            }

            return $products;       
        }
    }



  }

  
 ?>


with a request for a product to the server, the product is displayed, everything is fine, the task is, when you click on the ajax checkbox, you need to get / filter the product that outputs this file Product.php I don’t understand how to take this data in the ajax request handler, do I need to access this file Product.php

ajax request
$(document).ready(function() {
           $('.brand').on('click', function(){  //изменяем отметку чекбокса
                 if($(this).prop('checked')){  //если от отмечен
                       // заносим в переменную name из чекбокса
                          var nameT=$(this).attr('name');
                          
                     // и отправляем  ajax - краткий POST запрос
                      $.ajax({
                       url:"models/fetch_data.php",
                       method:"POST",
                       dataType: 'json',
                        data:{nP:nameT}, // будем отправлять nP в котором данные из чекбокса
                        success:function(data){
                            $('.product-list').html(data);
                        }
                  });

 }
});
  });


fetch_data.php file
$nP =  $_POST['nP'];
if ($nP == "Классические роллы") {
echo " Куртка, пинджак с отливом, брюки в полоску <br/>"; 
}
else if ($nP == "Суши"){
    echo "Штиблеты, туфли, тапки <br/>";
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
ThunderCat, 2020-05-20
@mutaev_murad

you need to get/filter the product when you click on the checkbox with ajax, which displays this Product.php file
1) Checkboxes that you filter?
2) There is no functionality in Product.php that takes into account more than one parameter, and that is only a category.
I don’t understand how to take this data in the ajax request handler
Poorly, it means that you still have little knowledge of your subject area. at the bottom of your question there is an example, this data is there, what to do with it depends on what you need to get.
do I need to refer to this Product.php file
In the presented form, it is useless for your task, you need to write a method for obtaining a list from arbitrary parameters. Then form an html with ready-made blocks from the list and send it (make an echo) back. Only in ajax it will be necessary to replacedataType: 'html'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question