如何在分页中保持单选按钮的选中值?

How will I make stay and hold the value of the checked radio button when I go to the next page? should I use session method? Here's my code:

include('includes/dbconn2.php');

// PAGINATION
if(isset($_GET['pageno'])){
    $pageno = $_GET['pageno'];
}else{
    $pageno =1;
}

$no_of_records_per_page = 5;
$offset = ($pageno-1) * $no_of_records_per_page; 
// get no of total no. pages
$total_pages_sql = "SELECT COUNT(item_id) AS total FROM items WHERE cat_id = 4";
$result = $conn->query($total_pages_sql);
$total_rows = $result->fetch_assoc();
// calculate total pages with results
$total_pages = ceil($total_rows['total'] / $no_of_records_per_page);
// sql query for pagination
$get_items = "SELECT item_id, item_name FROM items WHERE cat_id = 4 LIMIT $offset, $no_of_records_per_page";
if($results=$conn->query($get_items)){
    while($row=$results->fetch_assoc()){
        $app_id=$row['item_id'];
        $app_name=$row['item_name'];
        ?>
        <tr> 
            <td><?php echo $app_name; ?> </td>
            <td> <label class="container"><input type="radio" name="<?php echo $app_name?>-<?php echo $app_id; ?>" value="P0"><span class="checkmark"></span></label> </td>
            <td> <label class="container"><input type="radio" name="<?php echo $app_name?>-<?php echo $app_id; ?>" value="P1"> <span class="checkmark"></span></label></td>
            <td> <label class="container"><input type="radio" name="<?php echo $app_name?>-<?php echo $app_id; ?>" value="P2"> <span class="checkmark"></span></label></td>             
        </tr>
        <?php                        
    }
    $results->close();
}
  • You could use $_SESSION["values"] = "SOMETHING";
  • Another method could be <input type="checkbox" name="pagination" value="SOMETHING" hidden> inside a form
  • And the last and most insecure resourse is sending info through URL like this <a href="nexturl.php?values=SOMETHING" and in the page that you want to get the info, you have to use $_GET['values']
  • after that, you can use inside the checkbox <input type="checkbox" name="whaTever" value="SOMEVALUE" checked>

This is a long example of editing the SESSION by doing click on the checkBOX. You can try it here, how the following code works and Download Files here

<?PHP session_start(); ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- NEED THE jquery.min.js TO USE AJAX-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>

  <input type="checkbox" name="vehicle1" value="Bike" onclick="sendinfo($(this).val());"> I have a bike<br>
  <input type="checkbox" name="vehicle2" value="Car" onclick="sendinfo($(this).val());"> I have a car<br>
  <input type="checkbox" name="vehicle3" value="Boat" onclick="sendinfo($(this).val());"> I have a boat<br><br>
<a href="othersite.php">OtherSite</a>
<script type="text/javascript">
        function sendinfo(text) {   
            var form_data = {
                is_ajax: 1,
                infodata: text
            };
//alert(form_data);
            $.ajax({
                type: "POST",
                url: "refresh.php",
                data: form_data,
                success: function(response) {
                    $('.container').html(response).fadeIn();
                }
            });
        }
    </script>

<div class="container"></div>

</body>
</html>

REFRESH.PHP

<?PHP
session_start();
if($_SESSION["pagination"]==""){
$_SESSION["pagination"]= $_REQUEST["infodata"];
}else{
$myArray = explode(' ', $_SESSION["pagination"]); $call=FALSE;
foreach ($myArray as $key => $value){if($value==$_REQUEST["infodata"]){$call=TRUE; $keys=$key;}}
if(!$call){$_SESSION["pagination"]=$_SESSION["pagination"].' '.$_REQUEST["infodata"];}
else{
if(count($myArray)==1){$_SESSION["pagination"]="";}else{
$i=0; $tmp="";
foreach ($myArray as $key => $value){if($key!=$keys){if($i>0){$tmp=$tmp." ".$value;}else{$tmp=$value;}$i++;}}
$_SESSION["pagination"]=$tmp;
}   
}
}
?>

OTHERSITE.PHP

<?PHP
session_start();
function checkit($checkname){
$myArray = explode(' ', $_SESSION["pagination"]);
foreach ($myArray as $key => $value){if($value==$checkname){return "checked";}}
}

?>

  <input type="checkbox" name="vehicle1" value="Bike" <?PHP echo checkit("Bike"); ?>> I have a bike<br>
  <input type="checkbox" name="vehicle2" value="Car" <?PHP echo checkit("Car"); ?>> I have a car<br>
  <input type="checkbox" name="vehicle3" value="Boat" <?PHP echo checkit("Boat"); ?>> I have a boat<br><br>

</div>