Two Laptop Bag*
The moose likes Servlets and the fly likes get the id /value of the checkBox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "get the id /value of the checkBox" Watch "get the id /value of the checkBox" New topic
Author

get the id /value of the checkBox

tashu Bairy
Greenhorn

Joined: Jun 22, 2005
Posts: 9
HI all
I have a problem . I am having html code genereation in the servlet where i have a checkbox , which when clicked will pass on the id , i am not able to do that, here is the code
pw.println( "<TD>" );
pw.println( "<input type=\"checkbox\" name=\"chkTsk\" value=\"");
pw.println(task.getId());
pw.println( "\"");
pw.println( " unchecked>" );
pw.println( "</TD>" );


when the ckeckbox is checked, its not being able to take the id. What could be the problem & solution?
Thanks
Sharad Agarwal
Ranch Hand

Joined: Sep 11, 2002
Posts: 167
Before submitting the form, do a 'View Source'. Do you see the ID in the form?


Alco-Haul: We move spirits.
Demented Deliberations of a Dilettante
tashu Bairy
Greenhorn

Joined: Jun 22, 2005
Posts: 9
yes, i do see the id in the view source. But the problem is there is more than one check boxes in the form, i might check few of them & then submit the form, while parsing the valuesi.e id associated with those check boxes, its giving arrayoutofindex error for those checkboxes which r not checked,
Sharad Agarwal
Ranch Hand

Joined: Sep 11, 2002
Posts: 167
Can you send the following code snippets:
1. HTML View Source (just the parts with the checkboxes)
2. JSP/Servlet Code (just the part that is trying to retrieve the checkbox parameters)

Note that this forum allows for UBB Code. There are buttons just below the text area where you can wrap items in the 'code' tags. That way, it is easier to read the code.
[ June 27, 2005: Message edited by: Sharad Agarwal ]
tashu Bairy
Greenhorn

Joined: Jun 22, 2005
Posts: 9
<FORM ACTION="/ResourceManagerWS/dratask" METHOD="POST">
<TR>
<TR>
<TD>
<input type="checkbox" name="chkTsk" value="
110
"
unchecked>
</TD>










String button = req.getParameter( "TaskSubmit");
if(button!=null&&button.equalsIgnoreCase( "submit")){
String[] chkBxValues = req.getParameterValues("chkTsk");
String[] lstBxValues = req.getParameterValues("status");
String[] txtBxValues = req.getParameterValues("reason");

for(int i=0;i<txtBxValues.length ;i++){
if(!(txtBxValues[i].equals(""))){

String chkBxValue=chkBxValues[i].trim();
String lstBxValue=lstBxValues[i];
String txtBxValue=txtBxValues[i];
long tskId= Long.valueOf(chkBxValue.trim()).longValue();
Task tb=WMHelper.getTask(tskId);
tb.setCompletionExplanation(txtBxValue);
}
}
}
tx.commit();
}
catch( Exception e )

if the first checkbox is clicked then pgm works well, if first is not checked & any checked there on , gives
Sharad Agarwal
Ranch Hand

Joined: Sep 11, 2002
Posts: 167
Originally posted by tashu db:

String[] chkBxValues = req.getParameterValues("chkTsk");
String[] txtBxValues = req.getParameterValues("reason");

for(int i=0;i<txtBxValues.length ;i++){
if(!(txtBxValues[i].equals(""))){

String chkBxValue=chkBxValues[i].trim();
String txtBxValue=txtBxValues[i];
long tskId= Long.valueOf(chkBxValue.trim()).longValue();
}
}


I have snipped through your code a bit to focus on the problem area. Note that you have a loop of size txtBxValues.length and you are trying to use the iterator as index for the chkBxValue array.

Naturally, if (chkBxValue.length < txtBxValues.length), your code could fail. I hope this will help you fix your issue.
tashu Bairy
Greenhorn

Joined: Jun 22, 2005
Posts: 9
Sharad
Thank you so much for your help. Infact the problem was infact the one you had mentioned
Sharad Agarwal
Ranch Hand

Joined: Sep 11, 2002
Posts: 167
Glad I could help. Keep coding and keep asking!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: get the id /value of the checkBox
 
Similar Threads
problem with error mapping
Checking Checkbox values in indexed form
Need Help with binding click events to table rows
Need usage help for getDocumentById
need help :: multiple rows update without primary key