Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can not pass variable using Checkbox?

Status
Not open for further replies.

awieland

Programmer
Apr 18, 2005
36
US
I have a single checkbox that will pass a 'Y' when it is checked and a 'N' when unchecked.
I can get the 'Y' to pass when the box is checked, but when the box is unchecked I can NOT get the variable 'N' to pass, it passes nothing only a blank variable causing the application to crash.

************************************************************

xsl:choose>
<xsl:when test="contains(MarriedFileSingle, 'Y')">
<INPUT type="checkbox" name="MarriedFileSingle" value="" checked="yes" onclick="javascript:marriedSingle()"/>If married, but withholding at single rate, select Single status and check here.
</xsl:when>

<xsl:eek:therwise>
<INPUT type="checkbox" name="MarriedFileSingle" value="Y" onclick="javascript:marriedSingle()" />If married, but withholding at single rate, select Single status and check here.
</xsl:eek:therwise>
</xsl:choose>

************************************************************

function marriedSingle(){
if (document.W4Form.MarriedFileSingle.checked==true){
document.W4Form.MarriedFileSingle.value = "Y";

}
else (document.W4Form.MarriedFileSingle.checked==false){
document.W4Form.MarriedFileSingle.value = "N";

}

}

************************************************************
 
checkbox values only get passed if they're checked. otherwise, there's no value.

create a hidden field.

Code:
<input type="hidden" name="myField" value="" />

simplify:

Code:
function marriedSingle(){
    var e = document.forms['WfForm'].elements;
    e['myField'].value = (e['MarriedFileSingle'].checked) ? "Y" : "N";
}

and then use your new hidden field when you process the page.

*cLFlaVA
----------------------------
[tt]a frickin' twelve-gauge, what do you think?[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 

If there are only ever two values, why not simply put error checking server side to detect for the absence of the "Y"?

Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
cFlaVA,

I tryed to hide the first input and when the page comes back after checking the box, the checkbox has disappeared.
If I hide the second input box then no checkbox appears to make a selection?

I used your function script and it worked but I am still not able to uncheck the box and send an 'N' value to the database without the application crashing?

Thanks for help in advance,

******************************************
<xsl:choose>
<xsl:when test="contains(MarriedFileSingle, 'Y')">
<INPUT type="hidden" name="MarriedFileSingle" value="" checked="yes" onclick="javascript:marriedSingle();" />If married, but withholding at single rate, select Single status and check here.

</xsl:when>
<xsl:eek:therwise>
<INPUT type="checkbox" name="MarriedFileSingle" value="Y" onclick="javascript:marriedSingle();" />If married, but withholding at single rate, select Single status and check here.
</xsl:eek:therwise>
</xsl:choose>

*******************************************

function marriedSingle(){
var e = document.forms['W4Form'].elements;
e['MarriedFileSingle'].value = (e['MarriedFileSingle'].checked) ? "Y" : "N";
}
 
well, no, you didn't try what i told you. instead, you're still trying to set the value of the checkbox in that function. and that value will be set. but if the checkbox is not checked, then it will not have a value.

*cLFlaVA
----------------------------
[tt]a frickin' twelve-gauge, what do you think?[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
cLFlaVA,

Could you be more specific and give me an examlpe, I do not understand your statement "instead, you're still trying to set the value of the checkbox in that function. and that value will be set. but if the checkbox is not checked, then it will not have a value".

Thank you
 
a checkbox always has a value associated with it. however, if the checkbox is not checked, then the value will not be passed. therefore, even though you are setting the value of the checkbox properly, the value will only be passed if the checkbox is checked.

maybe two radio buttons would be more beneficial to you. this way, you could set one of them to be selected when the page loads, and the radio button field will therefore always have a value.

either that, or do what BullyRayPreachersSon said, and do the value setting on the server side.

*cLFlaVA
----------------------------
[tt]a frickin' twelve-gauge, what do you think?[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
cFlaVA,

Thanks, I now understand! I have to use a checkbox because I am mirroring functionality in PeopleSoft. Is there a way to set a value to the checkbox if it is NOT checked, on the client side? Do you know how to adjust my JavaScript to allow this to happen?
I can not do it on the server side because my application is not coded that way.
(How do you do it on the server side)?

Thanks, awieland
 
Maybe using a radio button instead would work better for you.

Adam

Whatever I feel like I wanna do, gosh!
 
my god-like self said:
maybe two radio buttons would be more beneficial to you. this way, you could set one of them to be selected when the page loads, and the radio button field will therefore always have a value.

*cLFlaVA
----------------------------
[tt]a frickin' twelve-gauge, what do you think?[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
Sorry, didn't see that.

Adam

Whatever I feel like I wanna do, gosh!
 
Not to be outdone, I suggest three radio buttons!

Adam

Whatever I feel like I wanna do, gosh!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top