vendredi 27 mars 2015

How to save checkbox state and post values to page after user clicks next pagination page or on page refresh?

What is the best way to save the state of checked checkboxes and POST the value of those checkboxes after a page refresh? In this situation I have multiple checkboxes to filter results from a MySQL database. Once a box is checked it updates my query and the results will display. The problem I am having is I am now adding pagination. When a user clicks to go to the next page of results, the checkboxes that were selected unchecks, and the php isn't reading that anything was posted and the query runs without the values of the checkbox included in the query.


How can I save the checked checkboxes and POST those values to my query even after page reload or after a user clicks to go to the next set of pagination results?


Here is html form:



<form id="form" method="post" action="">

<input type="checkbox" name="2kandunder" id="2kandunder" onchange="$('#form').submit();" class="checkbox" id="2kandunder" <?=(isset($_POST['2kandunder'])?' checked':'')?>/>

<input type="checkbox" name="2kto4k" id="2kandunder" class="checkbox" onchange="$('#form').submit();" <?=(isset($_POST['2kto4k'])?' checked':'')?>/>

<input type="checkbox" name="4kandup" id="4kandup" class="checkbox" onchange="$('#form').submit();" <?=(isset($_POST['4kandup'])?' checked':'')?>/>

</form>


And the PHP to post checkbox values into a string and then into a query to filter results.



<?php
if (isset($_POST["2kandunder"])) {
$arguments[] = "AND `2kandunder` = 'yes'";
}
if (isset($_POST["2kto4k"])) {
$arguments[] = "AND `2kto4k` = 'yes'";
}
if (isset($_POST["4kandup"])) {
$arguments[] = "AND 4kandup = 'yes'";
}
if(!empty($arguments)) {
$str = implode($arguments);
}
?>


Is it possible to use sessions for this?





Aucun commentaire:

Enregistrer un commentaire