473,383 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Retaining checkbox state on submit of page

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
4 8157
Atli
5,058 Expert 4TB
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
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
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
5,058 Expert 4TB
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

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

Similar topics

4
by: Jack | last post by:
Hi, I have a checkbox the value which goes to a database via a asp page that builds the sql string. In the front end asp page, the checkbox code is written as follows: <i><input...
13
by: aundro | last post by:
Hello, I've been looking on the web for a solution to this problem: I create a set of checkboxes, and 2 buttons: - one is labeled "All" - the other is labeled "None" Clicking "All" is...
0
by: Earl Teigrob | last post by:
I have a page that reads values from an XML file to display to the user. The page also has a control panel that allows administrators to update the XML file with new values. When an administrator...
5
by: Leo J. Hart IV | last post by:
Hello, I'm hoping someone can help me out. I was wondering if the Enabled property of a CheckBox or RadioButton server control is stored in the ViewState. If not, is there some way to to add...
1
by: Scott H | last post by:
I have a page with several server controls placed on there at design time, including a blank table control. During runtime I add some controls to the table, including some checkboxes, which I...
34
by: clinttoris | last post by:
Hello Experts, I have been told to post this in the Javascript forum as I want to do this client side just before my form gets submitted. Once the user clicks the submit button a javascript...
4
by: Jankie | last post by:
Greetings everyone ! I seek some help to retain checkbox state on redisplay.I only have one optional checkbox to indicate whether it's the user's first visit to the site Form1: <INPUT...
10
by: LionsDome | last post by:
Hello, I have a vb.net page which a bunch of checkboxes. A user can select a checkbox(s) and hit the submit button to store those values in a SQL Server table. This works fine with no problem...
13
by: ramprakashjava | last post by:
hi, i hav "java.lang.NullPointerException" error while Deleting table records using checkbox in jsp here i enclosed files help quickly plzzz.. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.