By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,664 Members | 1,643 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,664 IT Pros & Developers. It's quick & easy.

Retaining checkbox state on submit of page

P: 3
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:-
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3. ?>
  4. <html>
  5. <head>
  6. </head>
  7. <body>
  8. <form name="form1" action="chk10.php" method="POST">
  9. <table>
  10. <?PHP
  11. for($j=1;$j<=10;$j++)
  12. {
  13. ?>
  14. <tr>
  15. <td>
  16. <input type="checkbox" name="chk[]" value=<?PHP echo $j ;?> <?PHP if(isset($_POST["chk"]){
  17. $a=$_POST["chk"];
  18.  for($i=1;$i<=10;$i++)
  19. {
  20.  $_SESSION['count'][$i]=$a[$i];
  21. }
  22.  for($i=1;$i<=10;$i++)
  23. {
  24.  if($_SESSION['count'][$i]==$a[$i])
  25.  {
  26.  echo 'checked="checked"' ;
  27.  }
  28. }
  29.  > <?PHP echo $j; ?>
  30. </td>
  31. </tr>
  32. <?PHP
  33. }
  34. ?>
  35. <tr>
  36. <td>
  37. <input type="submit" name="btn" value="save" />
  38. </td>
  39. </tr>
  40.  
  41. <tr>
  42. <td>
  43. <a href="session1.php">Show session content</a>
  44. </td>
  45. </tr>
  46.  
  47.  
  48. </table>
  49. </form>
  50. </body>
  51. </html>
Sep 1 '08 #1
Share this Question
Share on Google+
4 Replies


Atli
Expert 5K+
P: 5,058
Hi.

Keep in mind that a checkbox will not be sent unless it is checked.
So using the isset function is a good way to see if a box was selected.
For example:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // Check if the box was sent.
  3. $checked = "";
  4. if(isset($_POST['box1'])) {
  5.   $checked = 'checked="checked"';
  6. }
  7.  
  8. // Print the form
  9. echo <<<END
  10. <form action="?" method="post">
  11.   <input type="checkbox" name="box1" $checked /> Testbox<br />
  12.   <input type="submit" />
  13. </form>
  14. END;
  15. ?>
  16.  
Sep 1 '08 #2

P: 3
Hii,
Thanks for ur quick reply. Now my exact question is that first i need to create the 10 checkboxes which i have already done and then when i set the value of any of the checkboxes as checked, it should be saved in a session after clicking on the submit button.After clicking on the submit button the values of the checkboxes that were checked should remain checked and should not be reset.And then after checking the values of checkboxes time and again on clicking on submit button, the checked values should be saved in session again such that the duplicate values are not allowed in the session. Since i am novice in php i have tried a lot but without success, please help out.
Sep 2 '08 #3

P: 3
My question is that first i need to create the 10 checkboxes which i have already done and then when i set the value of any of the checkboxes as checked, it should be saved in a session after clicking on the submit button.After clicking on the submit button the values of the checkboxes that were checked should remain checked and should not be reset.And then after checking the values of checkboxes time and again on clicking on submit button, the checked values should be saved in session again such that the duplicate values are not allowed in the session. Since i am novice in php i have tried a lot but without success, please help out.
The code that i have tried is below:-
Expand|Select|Wrap|Line Numbers
  1.  code goes here...
  2. <?php
  3. session_start();
  4. ?>
  5. <html>
  6. <head>
  7. </head>
  8. <body>
  9. <form name="form1" action="chk10.php" method="POST">
  10. <table>
  11. <?PHP
  12. for($j=1;$j<=10;$j++)
  13. {
  14. ?>
  15. <tr>
  16. <td>
  17. <input type="checkbox" name="chk[]" value=<?PHP echo $j ;?> <?PHP if(isset($_POST["chk"]){
  18. $a=$_POST["chk"];
  19.  for($i=1;$i<=10;$i++)
  20. {
  21.  $_SESSION['count'][$i]=$a[$i];
  22. }
  23.  for($i=1;$i<=10;$i++)
  24. {
  25.  if($_SESSION['count'][$i]==$a[$i])
  26.  {
  27.  echo 'checked="checked"' ;
  28.  }
  29. }
  30.  > <?PHP echo $j; ?>
  31. </td>
  32. </tr>
  33. <?PHP
  34. }
  35. ?>
  36. <tr>
  37. <td>
  38. <input type="submit" name="btn" value="save" />
  39. </td>
  40. </tr>
  41.  
  42. <tr>
  43. <td>
  44. <a href="session1.php">Show session content</a>
  45. </td>
  46. </tr>
  47.  
  48.  
  49. </table>
  50. </form>
  51. </body>
  52. </html>
  53.  
Sep 2 '08 #4

Atli
Expert 5K+
P: 5,058
Hi.

Do not double post your questions. This only serves to cause confusion and extra work for us moderators.

I have merged your new thread into this one.

Please read the Posting Guidelines before posting.

Thank you.
MODERATOR
Sep 2 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.