How to maintain status of Check Box in Multiple Pages/Pagination

Problem:- How to maintain the status of checkbox on multiple pages/Pagination in a webpage.

Technology :- PHP,AJAX


Solution:

First Page "test.php"- Simple Page, Check box are dynamically generated by PHP code, contains a link for next page and Java-script to be included in this page


<input type="submit" value="Check All" onclick="javascript:checkedAll(frm1)">Â
<input type="submit" value="Clear All" onclick="javascript:clearAll(frm1)"><br/>
<form name="frm1" id="frm1" action="" method="POST">
<?php for($i=1;$i<=5;$i++)
{
if(strpos($_SESSION['views'],$i."|")===false)
{
$s="";
}
else
{
$s="checked=true";
}
echo "<input type=\"checkbox\" value=\"$i\" ".$s." onClick=\"javascript:xyz($i)\"/><br/>";
}
?>
</form>
<!--<p>Checkbox:<span id="txtHint"></span></p>-->
<a href="test1.php">Next</a>

Second Page "test1.php" Similar to previous page, contains a link for previous page


<input type="submit" value="Check All" onclick="javascript:checkedAll(frm1)">&nbsp;
<input type="submit" value="Clear All" onclick="javascript:clearAll(frm1)"><br/>
<form name="frm1" id="frm1" action="" method="POST">
<?php for($i=6;$i<=10;$i++)
{
if(strpos($_SESSION['views'],$i."|")===false)
{
$s="";
}
else
{
$s="checked=true";
}
echo "<input type=\"checkbox\" value=\"$i\" ".$s." onClick=\"javascript:xyz($i)\"/><br/>";
}
?>
</form>

<!--<p>Checkbox:<span id="txtHint"></span></p>-->
<a href="test.php">Previous</a>

Third file javascript - To be included in both the above pages, this page contains the AJAX code, when checkbox is clicked, it goes to AJAX to store the value in PHP session


function xyz(value) { //For checking/unchecking single checkbox
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari

xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getvalue.php?q="+value,true);
xmlhttp.send();
}

function checkedAll (frm1) { //For CheckAll button
var aa= document.getElementById('frm1');
checked=true;
var cboxes="";

for (var i =0; i < aa.elements.length; i++)
{
if(aa.elements[i].checked==false)
{
aa.elements[i].checked = checked;if(!isNaN(aa.elements[i].value)){ cboxes += aa.elements[i].value+"|";}
}
}

if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari

xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getvalue.php?q1="+cboxes,true);
xmlhttp.send();
}

function clearAll (frm1) { //For Clear All Button
var aa= document.getElementById('frm1');
checked = false;
var cboxes1="";
for (var i =0; i < aa.elements.length; i++)
{
aa.elements[i].checked = checked;if(!isNaN(aa.elements[i].value)){
cboxes1 += aa.elements[i].value+"|";}
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getvalue.php?q2="+cboxes1,true);
xmlhttp.send();
}
Fourth File (getvalue.php) This contains the code to add or remove the checkbox value in PHP session


<?php
session_start();
// For single value
if(isset($_GET["q"])) //get checkbox value from ajax
{
$q=$_GET["q"];
if(strpos($_SESSION['views'],$q."|")===false)
{
$_SESSION['views'] .= $q."|"; //add value to session if not present
}
else
{
$string=str_replace($q."|","",$_SESSION['views']); //remove value from session if already present
$_SESSION['views']=$string;
}

}

//For Check All option
if(isset($_GET["q1"]) and $_GET["q1"] != "")
{
$q1=$_GET["q1"];
$arr=array();
$arr=explode("|",$q1);
foreach($arr as $i => $value)
{
if($arr[$i]!="")
{
if(strpos($_SESSION['views'],$arr[$i]."|")===false)
{
$_SESSION['views'] .= $arr[$i]."|"; //Add all the values checked one by one
}
}
}
}
//For Un Check All option
if(isset($_GET["q2"]) and $_GET["q2"] != "")
{
$q2=$_GET["q2"];
$arr1=array();
$arr1=explode("|",$q2);
foreach($arr1 as $i => $value)
{
if($arr1[$i]!="")
{
$string1=str_replace($arr1[$i]."|","",$_SESSION['views']); //Remove all the values one by ones
$_SESSION['views']=$string1;

}
}
}
echo $_SESSION['views'];
?>


No comments:

Post a Comment