My Code:
//html
<div class="addto-search">
<div class="form-input">
<input type="text" name="search_box" id="search_box" class="form-control" placeholder="Type your search query here" />
</div>
</div>
<div class="addto-playlists row" id="dynamic_content"></div>
<div id="checkbox-list">
<div>
<label class="filter"> Assigned Categories</label>
</div>
<div class="table-wrap">
<table class="table" style="width:100%">
<thead class="thead-primary">
<tr>
<th>Sl.</th>
<th>Category</th>
<th>Display Name</th>
<th>Display Order</th>
</tr>
</thead>
<tbody id="Category-container" class="group-containers">
</tbody>
</table>
</div>
</div>
//script
<script type="text/javascript">
$(document).ready(function(){
load_data(1);
function load_data(page, query = '')
{
$.ajax({
url:"category-filter.php",
method:"POST",
data:{page:page, query:query},
success:function(data)
{
$('#dynamic_content').html(data);
}
});
}
$(document).on('click', '.page-link', function(){
var page = $(this).data('page_number');
var query = $('#search_box').val();
load_data(page, query);
});
$('#search_box').keyup(function(){
var query = $('#search_box').val();
load_data(1, query);
});
});
</script>
<script type="text/javascript">
$(function() {
var $groupContainers = $('.group-containers'),
userChoices = [];
$('input:checkbox').on('change', function() {
var $checkedBox = $(this),
checkedBoxLabel = $checkedBox.parent('label').text(),
checkedValue = $checkedBox.val(),
category = $checkedBox.data('category'),
alreadyChecked = !$checkedBox[0].checked;
if(alreadyChecked){
removeChoiceFromModel(category, checkedValue);
} else {
addChoiceToModel(category, checkedValue);
}
updateDom();
return;
function cleanGroupContainers(){
$groupContainers.each(function(){
var list = $(this).find('tr');
if(list.length){
list.remove();
}
});
}
function updateDom(){
cleanGroupContainers();
appendChoices();
hideEmptyLists();
}
function appendChoices(){
var i, option, $categoryDiv,
len = userChoices.length;
if(!len) return false;
for(i = 0; i < len; i++){
option = userChoices[i];
$categoryDiv = $('#' + option.category + '-container');
j=i+1;
$categoryDiv.append('<tr><td>'+ j +'</td><td>'+ option.value +'</td><td><input type="text" value="'+option.value+'"></td><td><input type="number" value="0"></td></tr>');
}
}
function hideEmptyLists(){
$groupContainers.each(function(){
var $container = $(this),
items = $container.find('tr > td');
if(!items.length){
$container.hide();
return true;
}
$container.show();
});
}
function addChoiceToModel(category, value){
userChoices.push({
category: category,
value: checkedValue
});
}
function removeChoiceFromModel(category, value){
var i, option,
len = userChoices.length;
if(!len) return false;
for(i = 0; i < len; i++){
option = userChoices[i];
if(option.category === category && option.value === value){
userChoices.splice(i, 1);
return;
}
}
}
});
});
</script>
//category-filter.php
<?php
$connect = new PDO("mysql:host=localhost; dbname=charges", "root", "");
$limit = '5';
$page = 1;
if($_POST['page'] > 1)
{
$start = (($_POST['page'] - 1) * $limit);
$page = $_POST['page'];
}
else
{
$start = 0;
}
$query = "
SELECT * FROM category_table
";
if($_POST['query'] != '')
{
$query .= '
WHERE category_name LIKE "%'.str_replace(' ', '%', $_POST['query']).'%"
';
}
$query .= 'ORDER BY id ASC ';
$filter_query = $query . 'LIMIT '.$start.', '.$limit.'';
$statement = $connect->prepare($query);
$statement->execute();
$total_data = $statement->rowCount();
$statement = $connect->prepare($filter_query);
$statement->execute();
$result = $statement->fetchAll();
$total_filter_data = $statement->rowCount();
$output = '';
if($total_data > 0)
{
foreach($result as $row)
{
$output .= '
<label><input type="checkbox" value="'.$row["category_name"].'" data-category="Category" name="category_name"> <span>'.$row["category_name"].'</span></label>
';
}
}
else
{
$output .= '
<p>No Data Found</p>
';
}
$output .= '
<ul class="pagination">
';
$total_links = ceil($total_data/$limit);
$previous_link = '';
$next_link = '';
$page_link = '';
//echo $total_links;
if($total_links > 4)
{
if($page < 5)
{
for($count = 1; $count <= 5; $count++)
{
$page_array[] = $count;
}
$page_array[] = '...';
$page_array[] = $total_links;
}
else
{
$end_limit = $total_links - 5;
if($page > $end_limit)
{
$page_array[] = 1;
$page_array[] = '...';
for($count = $end_limit; $count <= $total_links; $count++)
{
$page_array[] = $count;
}
}
else
{
$page_array[] = 1;
$page_array[] = '...';
for($count = $page - 1; $count <= $page + 1; $count++)
{
$page_array[] = $count;
}
$page_array[] = '...';
$page_array[] = $total_links;
}
}
}
else
{
for($count = 1; $count <= $total_links; $count++)
{
$page_array[] = $count;
}
}
if(!$total_data == 0) {
for($count = 0; $count < count($page_array); $count++)
{
if($page == $page_array[$count])
{
$page_link .= '
<li class="page-item active">
<a class="page-link" href="#">'.$page_array[$count].' <span class="sr-only">(current)</span></a>
</li>
';
$previous_id = $page_array[$count] - 1;
if($previous_id > 0)
{
$previous_link = '<li class="page-item"><a class="page-link" href="javascript:void(0)" data-page_number="'.$previous_id.'">Previous</a></li>';
}
else
{
$previous_link = '
<li class="page-item disabled">
<a class="page-link" href="#">Previous</a>
</li>
';
}
$next_id = $page_array[$count] + 1;
if($next_id >= $total_links)
{
$next_link = '
<li class="page-item disabled">
<a class="page-link" href="#">Next</a>
</li>
';
}
else
{
$next_link = '<li class="page-item"><a class="page-link" href="javascript:void(0)" data-page_number="'.$next_id.'">Next</a></li>';
}
}
else
{
if($page_array[$count] == '...')
{
$page_link .= '
<li class="page-item disabled">
<a class="page-link" href="#">...</a>
</li>
';
}
else
{
$page_link .= '
<li class="page-item"><a class="page-link" href="javascript:void(0)" data-page_number="'.$page_array[$count].'">'.$page_array[$count].'</a></li>
';
}
}
}
}
$output .= $previous_link . $page_link . $next_link;
$output .= '
</ul>
';
echo $output;
?>
In the above code, live search from database works fine. but when items from checkbox selected, the respective category data should be displayed in the table and when checkbox is unselected, category data should be hidden, which is not working in above code.
If I remove code for the search and pagination that is ajax code, checkbox works fine.
I think input value fetched from thecategory-filter.php
is not able to use in main code.
Expected Ouput:
Can anyone please tell how can I fix this issue?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
DBMS is a database management system. It is designed to change, search, add and delete information in the database. There are many DBMSs designed for similar purposes with different features. One of the most popular is MySQL.
It is a software tool designed to work with relational SQL databases. It is easy to learn even for site owners who are not professional programmers or administrators. MySQL DBMS also allows you to export and import data, which is convenient when moving large amounts of information.
https://www.mysql.com/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.