Naruto Naruto - 1 year ago 136
PHP Question

Paging with multiple checkbox in PHP

Good day
I have an html table and I use paging on it so that only certain amount of items is shown. The problem is that I need to have a multiple selection with checkboxes and that works for a single page but I need that to work between pages. So for example on page 1 you choose 3 items and in the next page you choose 5 items and when POST happens I need to have all those items in one place so that I can store them in the database.

I could store data in session and then when a page is chosen I get the data for that page from session and mark all items that were in session as check and set there quantity from the session too. I suppose this could work but I just don't like. So I am interested if there is some other way to do that in PHP.

Here is some code:

$num_rec_per_page = 10;
if (isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page = 1;

$start_from = ($page - 1) * $num_rec_per_page;
$allProductsForPaging = GetAllProductsForPaging($start_from, $num_rec_per_page);

<form action="controllers/newOrderDetails.php" method="POST">
<div><label>Datum dostave:</label><input type="text" id="Date" name="Date" required="required"></div>
<table id="Products">
<th><input type="checkbox" id="CheckAll" /></th>
<th>Naziv proizvoda</th>
while ($row = $allProductsForPaging->fetch_assoc()) {
<td><input type="checkbox" class="ProductCheckBox" name="Product[]" value="<?php echo $row["ID"] . '|' . $row["ProductNameCroatian"] . '|' . $row["PriceInHRK"] . '|' . $row["PriceInEUR"]; ?>" /></td>
<td><?php echo $row["ProductNameCroatian"]; ?></td><td><input id="<?php echo $row["ID"] . 'Quantity'; ?>" name="<?php echo $row["ID"] . 'Quantity'; ?>" class="quantity" type="number" min="1" value="1" step="1" readonly/></td>
<td><?php echo $row["PriceInHRK"]; ?></td>


$allProducts = GetAllProducts();
$total_records = $allProducts->num_rows;
$total_pages = ceil($total_records / $num_rec_per_page);

echo "<a href='napraviNovuAdminNarudzbu.php?page=1'>" . '|<' . "</a> ";
for ($i = 1; $i <= $total_pages; $i++) {
echo "<a href='napraviNovuAdminNarudzbu.php?page=" . $i . "'>" . $i . "</a> ";
echo "<a href='napraviNovuAdminNarudzbu.php?page=$total_pages'>" . '>|' . "</a> ";
<input type="submit" value="Naruči" />

Thank you

I decided to use javascript to get the data, ajax to send them and session to store them.
Thx every one

Answer Source

Just to clearify your problem:

you want to modify a certain state of your website on user side, and whenever the user clicks on "next page" you want to submit your values to the server.

There are multiple ways, I would solve this:

  1. Consider using Ajax (google for it), and send the selection of the user immediately to the server. Benefit: You would have all changes always on server side, even if the user only selects, and never clicks on next page. Downside: You would have to implement the whole ajax-thing.

  2. Use a html-form for the whole section, and whenever the user clicks on "next page" your fields, which have to be part of the form, are submitted. Benefit: You do not have to do any javascript. Downside: You cannot send a state without the user clicking on next page and you have to restructure your html

  3. Use cookies and modify their value with javascript. Cookies are sent with every request to the server. Benefit: You do not have to restructure your code, just add some small javascript tokens. Downside: You cannot send an immediate state.

If you can describe your situation a little bit more in detail, I can provide you with a more detailed answer.

After your more detailed explanation in the comments, you should do something like that:

<form method="POST" action=".">
  <input type="checkbox" name="vehicle" value="Bike"> I have a bike<br>
  <input type="checkbox" name="vehicle" value="Car" checked="checked"> I have a car
  <table> <!-- your data --> </table>
  <input type="button" value="Previous Page" onClick="function() { form.action="previousPage.php"; form.submit(); }" />
  <input type="button" value="Next Page" onClick="function() { form.action="nextPage.php"; form.submit(); }" />

You have to take care in your PHP-Code to set the right checked="checked" flags

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download