I want to retain the state of checkboxes after the submit button has been clicked.
What it is currently doing is that it is resetting the checkboxes after the submit button is clicked.
My code is given below:-[code=php]
<?php
session_start() ;
?>
<html>
<head>
</head>
<body>
<form name="form1" action="chk10.p hp" method="POST">
<table>
<?PHP
for($j=1;$j<=10 ;$j++)
{
?>
<tr>
<td>
<input type="checkbox" name="chk[]" value=<?PHP echo $j ;?> <?PHP if(isset($_POST["chk"]){
$a=$_POST["chk"];
for($i=1;$i<=10 ;$i++)
{
$_SESSION['count'][$i]=$a[$i];
}
for($i=1;$i<=10 ;$i++)
{
if($_SESSION['count'][$i]==$a[$i])
{
echo 'checked="check ed"' ;
}
}
> <?PHP echo $j; ?>
</td>
</tr>
<?PHP
}
?>
<tr>
<td>
<input type="submit" name="btn" value="save" />
</td>
</tr>
<tr>
<td>
<a href="session1. php">Show session content</a>
</td>
</tr>
</table>
</form>
</body>
</html>[/code]
What it is currently doing is that it is resetting the checkboxes after the submit button is clicked.
My code is given below:-[code=php]
<?php
session_start() ;
?>
<html>
<head>
</head>
<body>
<form name="form1" action="chk10.p hp" method="POST">
<table>
<?PHP
for($j=1;$j<=10 ;$j++)
{
?>
<tr>
<td>
<input type="checkbox" name="chk[]" value=<?PHP echo $j ;?> <?PHP if(isset($_POST["chk"]){
$a=$_POST["chk"];
for($i=1;$i<=10 ;$i++)
{
$_SESSION['count'][$i]=$a[$i];
}
for($i=1;$i<=10 ;$i++)
{
if($_SESSION['count'][$i]==$a[$i])
{
echo 'checked="check ed"' ;
}
}
> <?PHP echo $j; ?>
</td>
</tr>
<?PHP
}
?>
<tr>
<td>
<input type="submit" name="btn" value="save" />
</td>
</tr>
<tr>
<td>
<a href="session1. php">Show session content</a>
</td>
</tr>
</table>
</form>
</body>
</html>[/code]
Comment