Skip to main content
Inspiring
April 18, 2006
Answered

How to pass checkboxes with values?

  • April 18, 2006
  • 3 replies
  • 871 views

I'm dynamically generating a list of items (say, vegetables), each with a numeric ID, and each with a checkbox. The user selects the vegetables he wants, and clicks the submit button. From the selected vegetable ID's, I want to do an insert into another table (say, customer orders.) I can't find any clear examples of how to do this. The form can submit to another .cfm page or self-submit, doesn't matter to me. Also, Flash forms are not allowed on my site.
Help, please! Thanks.
This topic has been closed for replies.
Correct answer ColdConFused

Thanks. The result page now does <cfif isDefined("FORM.vegetableID")> before processing, otherwise it shows "You have not selected any vegetables."

3 replies

Inspiring
April 18, 2006
That's as good as any code I would have written.

By the way, did you make sure at least one vegetable was selected first? Prevent more crashes that way.
ColdConFusedAuthorCorrect answer
Inspiring
April 19, 2006

Thanks. The result page now does <cfif isDefined("FORM.vegetableID")> before processing, otherwise it shows "You have not selected any vegetables."
Inspiring
April 18, 2006
Thanks! I suppose I'm halfway there now, but one more thing: How do I spin through the list of id's in my results/action page?
Inspiring
April 18, 2006
<cfloop> comes to mind. The cfml reference manual shows you how to loop through a list. If you don't have one, the internet does.
Inspiring
April 18, 2006
Sorry, I guess I should provide a little more info about my level of CF experience.

I've got a fairly good grasp of the basics, and I suspected cfloop would probably be involved, but the real question is what sort of structure will contain those values once the form is submitted, and what will execute within the cfloop to get each value? how is the structure indexed? etc.
Inspiring
April 18, 2006
For your form, do something like this:
<cfoutput query="vegetables">
<input type="checkbox" name = "vegetable" value="#vegetable_id#">#vegetable_name#

In your results page, form.vegetable will either be undefined (if nothing was checked), or a list of the id's you want.