Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Checkbox problem - checkboxes submitting that are unchecked

Reply
Thread Tools

Checkbox problem - checkboxes submitting that are unchecked

 
 
Claire
Guest
Posts: n/a
 
      02-28-2005
Hello,

I am having a problem in my struts application with the checkboxes in
my form. I have an array of checkboxes, some of which may be already
selected when the form loads. My problem is when the user then
deselects one or more of these checkboxes and submits the form -
they're being submitted as if they were still checked, when the user
has infact deselected them.

I've heard that this is a common problem, but I don't seem to be able
to find the problem / resolution with any search criteria. If anybody
knows the fix for this it would be greatly appreciated (if there is a
solution without using Javascript I would prefer this as we're trying
to write the application so it's not too dependant on Javascript . . .)

Many Thanks, Claire

Here's the HTML for the checkbox part of the form when it's initially
loaded.

<table style="width:50%;" cellpadding="0" cellspacing="0">
<tr class="spreadsheetvalue">
<td align="left" width="85%">Red</td>
<td align="left" width="15%">
<input type="checkbox" name="cust[0].add" value="on"
checked="checked">
</td>
</tr>
<tr class="spreadsheetvalue">
<td align="left" width="85%">Orange</td>
<td align="left" width="15%">
<input type="checkbox" name="cust[1].add" value="on"
checked="checked">
</td>
</tr>
<tr class="spreadsheetvalue">
<td align="left" width="85%">Yellow</td>
<td align="left" width="15%">
<input type="checkbox" name="cust[2].add" value="on"
checked="checked">
</td>
</tr>
<tr class="spreadsheetvalue">
<td align="left" width="85%">Green</td>
<td align="left" width="15%">
<input type="checkbox" name="cust[3].add" value="on">
</td>
</tr>
<tr class="spreadsheetvalue">
<td align="left" width="85%">Blue</td>
<td align="left" width="15%">
<input type="checkbox" name="cust[4].add" value="on">
</td>
</tr>
<tr class="spreadsheetvalue">
<td align="left" width="85%">Purpler</td>
<td align="left" width="15%">
<input type="checkbox" name="cust[5].add" value="on">
</td>
</tr>
</table>

 
Reply With Quote
 
 
 
 
Fred Oz
Guest
Posts: n/a
 
      02-28-2005
Claire wrote:
> Hello,
>
> I am having a problem in my struts application with the checkboxes in
> my form. I have an array of checkboxes, some of which may be already
> selected when the form loads. My problem is when the user then
> deselects one or more of these checkboxes and submits the form -
> they're being submitted as if they were still checked, when the user
> has infact deselected them.
>


Works as expected in Safari 1.0.3, IE 5.2 and Firefox 1.0 on Mac
OS X - only checked inputs are submitted.

So this is pure conjecture ... you may want to try using plain
checked instead of checked="checked" if your doctype is
HTML 4.01.

You may want to change the value to "yes" (or anything other
than "on"), as perhaps some browsers are being confused by your
setting the initial value to "on".

[...]

A slimmer version:

<form action="">
cust0<input type="checkbox" name="cust[0].add" value="on"
checked ><br>
cust1<input type="checkbox" name="cust[1].add" value="on"
checked ><br>
cust2<input type="checkbox" name="cust[2].add" value="on"
checked ><br>
cust3<input type="checkbox" name="cust[3].add" value="on"><br>
cust4<input type="checkbox" name="cust[4].add" value="on"><br>
cust5<input type="checkbox" name="cust[5].add" value="on"><br>
<input type="submit">&nbsp;<input type="reset">
</form>



--
Fred
 
Reply With Quote
 
 
 
 
Claire
Guest
Posts: n/a
 
      02-28-2005
Hello,

I don't think I can actually control what is output there as I am using
Struts and JSTL which produces the HTML for me, this is the actual
Struts / JSTL code:

<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/c" prefix="c" %>

<table style="width:50%;" cellpadding="0" cellspacing="0">

<c:forEach
var="customers"
items="${sessionScope.rescheduleForm.map.customers }"
varStatus="status">

<tr class="spreadsheetvalue">
<td align="left" width="85%">

<html:text
name="customers"
property="customerName"
readonly="true"
size="25" />

</td>
<td align="left" width="15%">

<html:checkbox
name="customers"
property="reschedule"
indexed="true" />

</td>
</tr>
</c:forEach>
</table>

The HTML I sent in the initial example I changed names to make it
easier to read, but I have not coded to produce HTML that says
checked="checked" or value="on"
the JSTL / Struts produces that.

Many Thanks, Claire

 
Reply With Quote
 
Matt Kruse
Guest
Posts: n/a
 
      03-01-2005
Claire wrote:
> I am having a problem in my struts application with the checkboxes in
> my form.


I suspect that your problem isn't checkboxes.
In struts, you need to reset your form object and make sure that your
checkbox values are set to false before populating the form from the
browser's input. Otherwise, you'll see them as true.

--
Matt Kruse
http://www.JavascriptToolbox.com


 
Reply With Quote
 
Claire
Guest
Posts: n/a
 
      03-02-2005
Hello,

Thanks for pointing me in the right direction. My problem was that I
had not implemented a

reset (ActionMapping mapping, HttpServletRequest request)

method. So for those who know Struts, I had to change my DynaActionForm
and create a normal Action Form which implemented a reset() method. The
reset method looped through the array, setting the boolean
(representing the checkbox) to false.

The reset() method is automatically called before my preload action to
prepolulate values (so only reset if the array isn't null, otherwise I
got a NullPointerException) and then again once the form is submitted
(the crucial bit, initialising checkboxes back to false and then
populating with the selected form values).

Many Thanks, Claire

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Xlint " warning: [unchecked] unchecked conversion" RVic Java 19 07-25-2009 08:40 PM
how to count number of unchecked checkboxes in a gridview? Dan ASP .Net 5 05-26-2009 09:07 AM
On submit checkbox member always null in ActionForm. Checkboxes display checked/unchecked properly on jsp/struts page. runescience Java 0 02-23-2006 08:14 PM
Checkbox problem - checkboxes submitting that are unchecked Claire Java 3 03-02-2005 05:17 PM



Advertisments