如何保持分页单选按钮的检查值?

时间:2018-10-16 03:21:53

标签: php mysql pagination

当我转到下一页时,如何保持并保持选中的单选按钮的值?我应该使用会话方法吗?这是我的代码:

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();
}

2 个答案:

答案 0 :(得分:0)

  • 您可以使用json.loads()
  • 另一种方法可以是表单中的$_SESSION["values"] = "SOMETHING";
  • 最后也是最不安全的资源是通过<input type="checkbox" name="pagination" value="SOMETHING" hidden>这样的URL发送信息,并且在要获取信息的页面中,您必须使用<a href="nexturl.php?values=SOMETHING"
  • 之后,您可以在复选框$_GET['values']
  • 中使用

答案 1 :(得分:0)

这是一个很长的示例,通过单击复选框来编辑SESSION。 您可以here, how the following code works 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>