I'm trying to submit an html form which includes check boxes for the days of the week. When they are checked, I pass a value of 1 for that check box. To handle unchecked boxes, I pass a value of 0 in a CFPARAM tag in the form action page.
I take these form inputs and add them to a structure which I pass to a webservice.
Form code (opentroubleticket.cfm):
<form action = "opentroubleticketaction.cfm" method="post" id="myForm" name="myForm">
<cfoutput><input type="checkbox" name="Loc_Mon" value="1"> MON </cfoutput>
<cfoutput><input type="checkbox" name="Loc_Tue" value="1"> TUE</cfoutput>
<cfoutput><input type="checkbox" name="Loc_Wed" value="1"> WED </cfoutput>
<cfoutput><input type="checkbox" name="Loc_Thu" value="1"> THU </cfoutput>
<cfoutput><input type="checkbox" name="Loc_Fri" value="1" > FRI</cfoutput>
<cfoutput><input type="checkbox" name="Loc_Sat" value="1" > SAT</cfoutput>
<cfoutput><input type="checkbox" name="Loc_Sun" value="1" > SUN</cfoutput>
<input type="submit" name="submit" value="SUBMIT" class="beef">
</form>
Action code (opentroubleticketaction.cfm):
<cfparam name="Loc_Mon" default="0">
<cfparam name="Loc_Tues" default="0">
<cfparam name="Loc_Wed" default="0">
<cfparam name="Loc_Thu" default="0">
<cfparam name="Loc_Fri" default="0">
<cfparam name="Loc_Sat" default="0">
<cfparam name="Loc_Sun" default="0">
<!---some other stuff--->
<cfscript>
BodyRT = structNew();
BodyRT.RepairRequest = structNew();
BodyRT.RepairRequest['Loc_Mon']=form.Loc_Mon;
BodyRT.RepairRequest['Loc_Tue']=form.Loc_Tue;
BodyRT.RepairRequest['Loc_Wed']=form.Loc_Wed;
BodyRT.RepairRequest['Loc_Thu']=form.Loc_Thu;
BodyRT.RepairRequest['Loc_Fri']=form.Loc_Fri;
BodyRT.RepairRequest['Loc_Sat']=form.Loc_Sat;
BodyRT.RepairRequest['Loc_Sun']=form.Loc_Sun;
</cfscript>
<!---some other stuff--->
When I submit the form with all checked boxes, it goes through and passes to the webservice correctly with value=1 for each input. When I submit the form with unchecked boxes, I get this error:
Element LOC_MON is undefined in FORM
Why is the element undefined, even if I set a default value with the CFPARAM
tag?
If I check the MON box, it says Element LOC_TUE is undefined in FORM
, and so on.
What you are seeing is the default behavior of check boxes in HTML forms. The values are only passed if the box(es) are checked. When unchecked, then the form fields are not passed.
As mentioned in the comments, the reason your <cfparam>
tags are not catching this condition is because you have not scoped them. This should fix your problem.
I also noticed in the comments that you said scoping did not fix the problem. That is because you also have a typo in your code (or at least a mis-match). In your <cfparam>
tag for the Tuesday variable you set the name as Loc_Tues
(notice the 's' on the end). Later in your code you are referring to Loc_Tue
(without an 's'). So add the form.
scope in your <cfparam>
tags and fix that mis-match then you should be good to go.
<cfparam name="form.Loc_Mon" default="0">
<cfparam name="form.Loc_Tue" default="0">
<cfparam name="form.Loc_Wed" default="0">
<cfparam name="form.Loc_Thu" default="0">
<cfparam name="form.Loc_Fri" default="0">
<cfparam name="form.Loc_Sat" default="0">
<cfparam name="form.Loc_Sun" default="0">