• Post Reply Bookmark Topic Watch Topic
  • New Topic

retrieve dynamically generated checkbox in JSP

 
vivien siu
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I'm going to display a list of checkboxes which is generated dynamically according to the database. This is the code I used for display:


After pressing the "update" button, the servlet will check whether the checkbox is checked or not, if is checked, then updates the database.

My question is how to use request.getParameter(name) for retrieving data from the checkbox, since the name field is inconsistent and subjected to the Vector size. Please help.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
since the name field is inconsistent and subjected to the Vector size.

According to the code you posted. the name of every checkbox control, as well as the submit button, is going to be update. And the value for each of the checkboxes will be the same: Submit.

Is that what you intended?
[ November 17, 2005: Message edited by: Bear Bibeault ]
 
vivien siu
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:

According to the code you posted. the name of every checkbox control, as well as the submit button, is going to be update. And the value for each of the checkboxes will be the same: Submit.

Is that what you intended?

[ November 17, 2005: Message edited by: Bear Bibeault ]


nope bear, each checkbox generated will be pointing to different rows in the database. Anyway here's the full code:

when the update button is clicked, it will scans through each row in the table to check whether the checkbox is checked. If it is checked, then the database will update the payment field using the payment.getPeoplewise() as the primary key.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nope bear, each checkbox generated will be pointing to different rows in the database.


Incorrect. According to the code you posted there is no way that the checkboxes created within the body of the loop with



will produce anything but identical elements.

If you are going to insist otherwise, there's not much I can do for you.

The following



is the only aspect that can be dynamic.

And why are you using out.println instead of a scriplet expression such as:

 
vivien siu
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi bear, the thing is I have no idea how I should code the checkboxes so bear with me if I got the checkbox line of code wrong.

I need guide on how should I code the line of dynamically generated checkboxes so that it not only can display according to the database but can also update the database when users click on the update button.
 
vivien siu
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:


will produce anything but identical elements.

If you are going to insist otherwise, there's not much I can do for you.

The following



is the only aspect that can be dynamic.

And why are you using out.println instead of a scriplet expression such as:



you are looking at somebody who just started on scriplets here, so it's not surprising if I got so many errors which I shouldn't be using at all. So bear with me on the errors OK??
 
vivien siu
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, this is the edited code:


on submit button, the following will be done:


for the meantime I'm able to retrieve which checkbox is selected, however only one checkbox can be retrieved although I checked on multiple boxes. So can anyone please advice me how can I retrieve all the checked values??

Also I have been getting pointers that I should be using struts instead on the internet. Is this true? Please advice.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
request.getParameterValues("payment") will return a String Array of values for all of the checkboxes with the name "payment" that were checked at the time of the form submission.
 
vivien siu
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
icic, will try that out, Thanks Bear!!!
EDIT: sorry left out somebody!! Thank you Ben! Have tried the code you give and it works like a charm!!
[ November 20, 2005: Message edited by: vivien siu ]
 
Vijay Venkat
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Over and above the HTML document that will be rendered is actually wrong. As per HTML spec, there cannot be two element having same id.


results in element with id's as 'payment' for all the checkboxes.

If there is no need for it, you can avoid using it.

If you are using JSP 2.0 then you should use Expression Language. See JSP 2.0 Spec.

Vijay Venkataraman
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!