Skip to main content
Participating Frequently
February 1, 2010
Question

create a list using checkboxes, count the elements, do math

  • February 1, 2010
  • 1 reply
  • 1739 views

Hello, everyone.  I'm a total coldfusion noob and I'm in over my head on a project.  I've been trying to figure it out for the past 24 hours to no avail.

I'm trying to create a pizza order form for a project.  I want to select the toppings on one page, then display the results in another page and do some math (the price of the pizza and the number of toppings, on the second page).  I've got the first page down, I believe. I cannot figure out how to count the number of pizza toppings that I wind up choosing, and how to compute a price for the pizza once I've selected the ingredients.

Here's what I have thus far:

Input page:

<body>
<form action="ex2output.cfm" method="post">
Choose the ingredients you would like on your pizza:
<br />
Black Olives: $.50
<input type="checkbox" name="BlackOlives" value="Yes"/>
<br />
Hot Peppers: $1.00
<input type="checkbox" name="HotPeppers" value="Yes"/>
<br />
Mushrooms $.50
<input type="checkbox" name="Mushrooms" value="Yes"/>
<br />
Onions $.50
<input type="checkbox" name="Onions" value="Yes"/>
</br>
<input type="submit" value="Submit"/>

</body>
</html>

Output page:

<body>
<cfparam name="BlackOlives" default="no">
<cfset BlackOlives=".50">
<cfparam name="HotPeppers" default="no">
<cfset HotPeppers="1">
<cfparam name="Mushrooms" default="no">
<cfset Mushrooms=".50">
<cfparam name="Onions" default="no">
<cfset Onions=".50">
<cfoutput>
Your order:
<li>
<cfif BlackOlives is "yes"> Black Olives
</cfif>
</li>
<li>
<cfif HotPeppers is "yes"> Hot Peppers
</cfif>
</li>
<li>
<cfif Mushrooms is "yes"> Mushrooms
</cfif>
</li>
<li>
<cfif Onions is "yes"> Onions
</cfif>
</li>
</cfoutput>
</ul>
</body>
</html>

Any and all help would be greatly appreciated,

Thank you,

R

    This topic has been closed for replies.

    1 reply

    Inspiring
    February 1, 2010

    I'd probably do something like this.  On the form page, give all the checkboxes the same name.  Use a pipe delimited list for the values.

    <input name="topping" type="checkbox" value="Black Olives|.5">Black Olives $.50

    etc

    On the action page, form.ingredients will either not exist at all, or will be a comma delimited list.  Each list element would be a pipe delimited list.

    That should get you started.

    RSuciuAuthor
    Participating Frequently
    February 2, 2010

    I hate being a noob. I don't understand what you're saying about the action page.  Particularly, once I've modified the first page, I can't get the action page to find the variables.  I know I'm naming them wrong, but I don't know how to fix it.  And I was under the impression that giving all the inputs the same name (i.e. topping would turn the buttons into radio buttons, whereby I could only make one selection out of the whole list as opposed to as many ingredients as I'd like?)

    Thank you,

    R

    Inspiring
    February 2, 2010

    When you submit a form that has checkboxes, for each checkbox name, one of two things happen.  If nothing with that name is checked, the form field will not exist on your action page.  Otherwise, all the values of the checked boxes will exist as a comma delimited list.

    On your form page, it's the type attribute of your input/cfinput tag that determines the type of input control.