Answer the question
In order to leave comments, you need to log in
Handling ajax request?
I have some MVC and it has a file
<?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;
}
}
}
?>
$(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);
}
});
}
});
});
$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
you need to get/filter the product when you click on the checkbox with ajax, which displays this Product.php file1) Checkboxes that you filter?
I don’t understand how to take this data in the ajax request handlerPoorly, 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 fileIn 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 replace
dataType: 'html'
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question