473,809 Members | 2,668 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Retaining checkbox state on submit of page

3 New Member
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 8184
Atli
5,058 Recognized Expert Expert
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
virk1711
3 New Member
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
virk1711
3 New Member
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 Recognized Expert Expert
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
4632
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 type="checkbox" name="chk_Complete" value="<%Response.Write l_IsChecked%>"<%if cbool(l_IsChecked) then Response.Write " checked"%>> The code to captures the checkbox value in the asp page that builds the sql string is follows
13
31489
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 supposed to check all the checkboxes, which it does (that's not rocket science ;), but the 'onchange' event does not get triggered.
0
1362
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 presses the update button in the control panel area of the page and the values are written to the XML file, I want the page to reload with the new values WHILE RETAINING ANY VIEW STATE VALUES. (that are not rewriten by the new XML values) I have...
5
9168
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 this property to ViewState? If needed, I can give you more info on exactly what I'm trying to do. Thanks,
1
3983
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 called ch() as checkbox. the user would then check some of them and click a button. the event handler for the button would save the checkbox checked state to a database. but as soon as the button is clicked, ch() is nothing. all the other
34
3843
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 function needs to run and validate all the checkboxes on my form and make sure none of them are unchecked. I suck at Javascript and my problem is 2fold. I have the following code that constructs the checkbox response.write "<input type=checkbox...
4
3351
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 type="checkbox" name="guest" value="yes">
10
4494
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 the first time the user submits. However when user submits a second time while changing some of the selected boxes the page only re-submits the previosly selected checkbox values. It's like its storing it somewhere in the cache or something. My...
13
26942
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.. Regards Ramprakash /*Adminpage.jsp*/ <%@ page language="java" import="java.sql.*"%> <html> <head>...
0
9722
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10378
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10391
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10121
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3862
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.