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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically Naming Dynamic Checkboxes

Status
Not open for further replies.

scripter73

Programmer
Apr 18, 2001
421
US
I have the following two CFLOOP statements:

<cfset vehcounter=0>
<CFLOOP QUERY=&quot;vehlook&quot;>
<cfset vehcounter = vehcounter + 1>
<cfset veh_entry = '#vehlook.wveh_vehicle_year# #vehlook.wveh_vehicle_make# #vehlook.wveh_vehicle_model# #vehlook.wveh_vehicle_id#'>
<input type=&quot;checkbox&quot; name=&quot;vehlist<cfoutput>#vehcounter#</cfoutput>&quot; value=&quot;<cfoutput>#veh_entry#</cfoutput>&quot;><b><cfoutput>#veh_e
ntry#</cfoutput></b><br>
</CFLOOP>


<cfset drvcounter = 0>
<CFLOOP QUERY=&quot;drvlook&quot;>
<cfset drvcounter = drvcounter+1>
<cfset drv_entry = '#drvlook.wdrv_driver_name#'>
<input type=&quot;checkbox&quot; name=&quot;drvlist<cfoutput>#drvcounter#</cfoutput>&quot;
value=&quot;<cfoutput>#drv_entry#</cfoutput>&quot;><b><cfoutput>#drv_e
ntry#</cfoutput>
<cfif drvlook.wdrv_excluded_ind eq 'Y'><cfoutput><font color=&quot;red&quot;>(Excluded)</font></cfoutput>
</cfif></b><br>
</CFLOOP>


The two query &quot;vehlook&quot; and &quot;drvlook&quot; are defined elsewhere in the code and are successful because I can see my dynamically created checkboxes.


I want to make sure I'm naming the text boxes the right thing. Preferably I want every checkbox to
correspond with its name like, vehlist1, vehlist2, and drvlist1, and drvlist2, etc.

When I display my form with this data, everything shows fine. However I have a button in my form
that when the user clicks on it, it is supposed to show the name of the checkboxes and its corre-
sponding value. This is a test to verify naming. Also my form's name is &quot;form1&quot;. This is in Javascript, and the code is as follows:


<script language=&quot;Javascript&quot;>
function display_names(){
//assigns the fields of the contact section the same fields as
//the insured section and refreshes the browser page

document.write(document.form1.vehlist1.name + &quot;= &quot;
+ document.form1.vehlist1.value);
document.write(document.form1.vehlist2.name + &quot;= &quot;
+ document.form1.vehlist2.value);
document.write(document.form1.drvlist1.name + &quot;= &quot;
+ document.form1.drvlist1.value);
}
// -->
</script>
}


When I click my 'Continue' button to process the Javascript, I only get a listing for vehlist1. What about the others like vehlist2 and drvlist1?


What am I doing wrong? I just want to make sure that I name my checkboxes correctly and
since they're dynamically created, I have to be a little creative to name them appropriately.

Thanks in advance,
scripter73
 
If the only goal is to make sure that your Dynamic Form fields are named correctly, rather then mess with the JavaScript, why not submit the values and test them.

From the looks of your code your variables are,correctly, going to be named

vehlist1, vehlist2, and drvlist1, and drvlist2

In order to test this on the action page you would use:
<CFLOOP from=&quot;1&quot; to=&quot;#maxfields#&quot; index=&quot;X&quot;>
<CFSET FormVar=&quot;vehlist#X#&quot;>
<CFSET FormVar=#Evaluate(FormVar)#>
<CFOUTPUT>#FormVar#</CFOUTPUT>
DO SOMETHING WITH FORMVAR
</CFLOOP>

This is a simplified view and doesn't show how to figure the maxfields variable (I usually send it as a hidden form variable but there are other ways) But if this doesn't work then chances are your other code is wrong, but I wouldn't worry about it so much. It looked fine.

Hope this Helps.
 
Hi tlhawkins,

Thanks for your assistance. I'm taking your advice. In my mainfile that contains the form, I have action=&quot;shownames.cfm&quot;, and in my shownames I have your code:

<CFLOOP from=&quot;1&quot; to=&quot;#maxfields#&quot; index=&quot;X&quot;>
<CFSET FormVar=&quot;vehlist#X#&quot;>
<CFSET FormVar=#Evaluate(FormVar)#>
<CFOUTPUT>#FormVar#</CFOUTPUT>
DO SOMETHING WITH FORMVAR
</CFLOOP>

My question is though, how do I go about accessing my queries like vehlook, or drvlook? This is where my data is. I tried something like #vehlook.maxfields# but I received a CF error like &quot;Error resolving parameter&quot;.

Am I putting your code in the right spot? Please advise.

Thanks,
scripter73
 
Hey scripter73,

It looks like you passed all of the Query info through your form fields as a string of values. IF there is more Query info, that would have to be re-queryied. But to access what info you have sent you will have to (Or can, I'm sure there are other ways) loop through them as a list. Like this:


IF You're Using CF4.5 this is the best way...

Code:
<CFSET wveh_Year=ArrayNew(1)>
<CFSET wveh_Make=ArrayNew(1)>
<CFSET wveh_Model=ArrayNew(1)>
<CFSET wveh_ID=ArrayNew(1)>

<cfloop collection=&quot;#Form#&quot; item=&quot;FormVar&quot;>
  
 <CFSET wveh_Year[#currentRow#]=#Listgetat(Form[FormVar],1,&quot; &quot;)#>
  <CFSET wveh_Make[#currentRow#]=#Listgetat(Form[FormVar],2,&quot; &quot;)#>
<CFSET wveh_Model[#currentRow#]=#Listgetat(Form[FormVar],3,&quot; &quot;)#>
<CFSET wveh_ID[#currentRow#]=#Listgetat(Form[FormVar],4,&quot; &quot;)#>

</cfloop>

You now have an array for each of your origninal Query variables that were all strung together into the Form variable with the #veh_entry# variable.

You can put them back the way they were with a collection but my mind was glitching on the code so I did it this way.

The <CFLOOP Collection=&quot;#form#&quot; ... part is used to access the form variables that were sent because with checkboxes if a box is not checked nothing is sent.

This code is not completely perfect in that it will error out when it comes to the drvlist? series of checkboxes. I would recomend testing the name of the variable in the loop before seperating it into it's parts. make sure the name contains vehlist or drvlist and work with it accordingly.

If you have any trouble with this part let me know, I'll see if I can help.





 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top