选中的复选框将保留在分页中 [英] checked checkbox will remain through pagination

查看:76
本文介绍了选中的复选框将保留在分页中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个PHP脚本,其中我需要记住选中的复选框并将其保存到所有数据库中。不幸的是,我的代码只保存当前页面,我选中了复选框,但另一个复选框变为未选中状态。

i am doing a php script wherein I need to remember the checked checkbox and save it all the database. Unfortunately, my code save only the current page where I checked the checkbox but the other checked box became unchecked.

示例在第1页中,我检查了3个项目,在第二页上我检查了我的项目。当我单击提交按钮时,我只获得当前页面的选中项目。当我回到上一页时,我检查的项目变为未选中状态。如何通过分页保存并保存已选中复选框的值?

Example In Page 1 I checked 3 items, on the second page I checked I tem. When I click the submit button I only got the checked item of the current page. And when I go back to the previous page the item that I checked became unchecked.How can I preserved and save the value of my checked checkbox through pagination?

这是我的CreateTest.php的代码

here is my code for CreateTest.php

<html>
<body>
<?php
ob_start();
session_start();
include("connect.php");
error_reporting(0);
$item_per_page=10;
$results = mysqli_query($con,"SELECT COUNT(*) FROM tblitem");
$get_total_rows = mysqli_fetch_array($results); //total records

//break total records into pages
$pages = ceil($get_total_rows[0]/$item_per_page);   

//create pagination
if($pages > 1)
{
    $pagination = '';
    $pagination .= '<ul class="paginate">';
    for($i = 1; $i<=$pages; $i++)
    {
        $pagination .= '<li><a href="#" class="paginate_click" id="'.$i.'page">'.$i.'</a></li>';
    }
    $pagination .= '</ul>';
}

?><!DOCTYPE html>

<script type="text/javascript">
$(document).ready(function() {
    $("#results").load("fetch_pages.php", {'page':0}, function() {$("#1-page").addClass('active');});  //initial page number to load

    $(".paginate_click").click(function (e) {

        $("#results").prepend('<div class="loading-indication"><img src="ajax-loader.gif" /> Loading...</div>');

        var clicked_id = $(this).attr("id").split("-"); //ID of clicked element, split() to get page number.
        var page_num = parseInt(clicked_id[0]); //clicked_id[0] holds the page number we need 

        $('.paginate_click').removeClass('active'); //remove any active class

        //post page number and load returned data into result element
        //notice (page_num-1), subtract 1 to get actual starting point
        $("#results").load("fetch_pages.php", {'page':(page_num-1)}, function(){

        });

        $(this).addClass('active'); //add active class to currently clicked element (style purpose)

        return false; //prevent going to herf link
    }); 
});
</script>
<form name="myform" action="CreateTest.php" method="POST" onsubmit="return checkTheBox();" autocomplete="off">

<body>
<?php 
if(isset($_POST['save'])){
    $testPrice = $_POST['testPrice'];
    $testName = $_POST['testName'];
    $items = $_POST['items'];
    $quantity = $_POST['quantity'];
    $testDept = $_POST['testDept'];
    $measurement = $_POST['measurement'];
 global $con;
   Tool::SP_Tests_Insert(strip_tags(ucwords($testName)), $testPrice, $testDept);
    $result = mysqli_query($con, "SELECT MAX(TestID) FROM lis.tbltests");
    $data=  mysqli_fetch_array($result);
    $testID=$data[0];
    foreach ($items as $key => $value){
     $checkedItem[] = $value; 
    echo $value, " | ",$quantity[$key], " | ",$measurement[$key], "<br>";
     mysqli_query($con,"INSERT INTO tbltestitem (TestID, ItemID, ItemQuantity, ItemMeasurement) VALUES ($testID, $value, '$quantity[$key]', '$measurement[$key]')");
}
echo "<script type='text/javascript'>alert('Succesfully added test!')</script>";
    $site_url = "tests.php";
    echo "<script language=\"JavaScript\">{location.href=\"$site_url\"; self.focus();  }</script>";

}else if(!isset($_POST['save'])){
$selectDept='';
$result= mysqli_query($con,"select * from tbldepartment");
$selectDept.="<option value=''>Select Department:</option>";
while($data = mysqli_fetch_array($result)){
    $selectDept.="<option value='{$data['DeptID']}'>{$data['DeptName']}</option>";
}
?>
<td style="vertical-align: top;">
    <body>   
    <div id="container" align="center">
        <div id="title">Create Test</div>
        <div id="a">Input Test Name:</div><div id="b"><input type="text" name="testName" id="myTextBox" onkeyup="saveValue();" ></div>
        <div id="a">Input Test Price:</div><div id="b"><input type="number" name="testPrice"></div>
         <div id="a">Select Department:</div><div id="b"><select name="testDept" ><?php echo $selectDept; ?></select></div>

    <div id="results"></div><div id="a"><?php echo $pagination; ?></div>
<div align="right" style="padding: 10px;"><input type="submit" name="save" value="Submit"></div> </div>
<?php
}
?>   
</body>
</html>

这是我的fetch_pages.php代码。
这个php页面帮助我通过jquery保持文本框值,它将被加载而不会进入另一页的分页

This is my fetch_pages.php code. this php page help me to keep the textbox values through pagination through jquery it will be loaded without going the another page of pagination

<?php

include("connect.php"); 
require_once('classes/tool.php');
$item_per_page=10;
//sanitize post value
$page_number = $_POST["page"];

//validate page number is really numaric
if(!is_numeric($page_number)){die('Invalid page number!');}

//get current starting point of records
$position = ($page_number * $item_per_page);

//Limit our results within a specified range. 
$results = mysqli_query($con,"SELECT * FROM tblitem ORDER BY ItemID ASC LIMIT $position, $item_per_page");
$connection=mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
$selectMeasure='';
$measurements = Tool::SP_Measurement_Select();    
foreach($measurements as $measure) {      
    $selectMeasure.='<option value=' . $measure['MeaName'] . '>' . $measure['MeaName'] . '</option>';
$i=0;
while($item = mysqli_fetch_array($results))
{
    echo "<div id='a'><input type='checkbox' name='items[$i]' id='item[]' value='". $item['ItemID'] ."' >".$item['ItemName']."</div>";
    echo "<div id='b'><input type='number' name='quantity[$i]' class='quantity' /></div>";        
    echo "<div id='b'><select name='measurement[$i]' class='quantity'>'".$selectMeasure."'</select></div>";
$i++;
}
?>

希望你能帮助我。在此先感谢

Hope you can help me. Thanks in advance

推荐答案

Ugg ......太多代码可以查看。

Ugg... way too much code to look through.

但简短的回答是,您使用< input type-hidden...> 标记将值从一个表单传递到另一个表单。

The short answer, however, is that you pass values from one form to another using <input type-"hidden"...> markup.

警告,代码类型徒手

Page1.php

<form action="page2.php">
  <div>
    <input type="checkbox" name="test1">
  </div>
</form>

Page2.php

<?php
  if (is_set($_REQUEST["test1"])) {
    $test1 = $_REQUEST["test1"];
  } else {
    $test1 = false;
  }

<form action="page3.php">
  <div>
    <input type="hidden" name="test1" value="<?php echo $test1 ?>">
  </div>
</form>

Page3.php

<?php
    $test1 = $_REQUEST["test1"];
?>

这篇关于选中的复选框将保留在分页中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆