提交带有多个复选框/选择菜单的表单信息 [英] Submit form information with multiple checkboxes/select menus
问题描述
我想知道在用户点击一次提交后,是否有可能将这种小提琴上的表格发布到数据库中。
I wanted to know if it was possible to have the form on this fiddle come out onto the database like this after the user hits submit once.
+------+------+----------+
| Name | Meal | Quantity |
+------+------+----------+
| Adam | Beef | 2 |
| Adam | Pork | 1 |
| Adam | Lamb | 3 |
+------+------+----------+
http://jsfiddle.net/f8zyakvj/1/
我知道通过多重选择,您选择的所有信息都以数组形式发送,但是对于复选框,我如何做到这一点,以便将信息以类似的时尚。当我勾选最上面的3个框并提醒食物价值来查看我得到的东西时,无论我勾选哪个框,我所得到的都是 1。
I know that through multi selects, all the information you select is sent through as an array, but for checkboxes, how can I make it so that the information is sent in a similar fashion. When I check off the top 3 boxes, and alert the value of food to see what I get, all I ever got is "1" no matter which box I check off.
我希望这会像多重选择一样容易,因为我可以为cfm做类似的事情
I was hoping it would be as easy as the multi select where I could do something like this for the cfm
<cfloop list="#form.meal#, #form.quantity#" index="currMeal, currQuant">
<cfquery name="Add" datasource="food">
INSERT INTO Log (Name, Meal, Quantity)
VALUES (
<cfqueryparam value="#Form.Name#" cfsqltype="cf_sql_varchar">
<cfqueryparam value="#currMeal#" cfsqltype="cf_sql_varchar">
<cfqueryparam value="#currQuant#" cfsqltype="cf_sql_integer">
)
</cfquery>
</cfloop>
对此问题的任何帮助将不胜感激。
Any help on the issue would be appreciated.
推荐答案
- 您的表单元素没有名称,因此这些值将不会发布到您的表单操作页面
- 对于您选择的下拉菜单,使用名称输入与餐复选框相对应的值,以便您可以轻松查找它-例如
<输入类型=复选框 value = 3 name = meal>
将与< select name = quantity_3配对>
- 您的插入查询缺少逗号,并且会给您一个SQL语法错误。
- Your form elements do not have names, so the values won't be posted to your form action page
- For your select drop downs, use the name to put the value corresponding to the meal checkbox so you can easily look it up - e.g.
<input type="checkbox" value="3" name="meal">
would be paired with<select name="quantity_3">
- Your insert query was missing commas, and would have given you a SQL syntax error.
示例:
<form name="form" action="Submit.cfm" method="post">
<input type="checkbox" value="1" name="meal"> Beef
<select name="quantity_1" class="GC0">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select><br>
<input type="checkbox" value="2" name="meal"> Chicken
<select name="quantity_2" class="GC1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select><br>
<input type="checkbox" value="3" name="meal"> Pork
<select name="quantity_3" class="GC2">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select><br>
<input type="checkbox" value="4" name="meal"> Lamb
<select name="quantity_4" class="GC3">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select><br>
Name: <input type="text" name="name" id="name">
<button value="submit">Submit</button>
</form>
然后在您的处理页面上 submit.cfm
您可以遍历FORM.meal(如果存在)并使用每个检查项目的值来引用相应的数量字段名称:
Then on your processing page submit.cfm
you can loop through FORM.meal (if it exists) and use the value of each item checked to reference the corresponding quantity field name:
<!--- Make sure something was checked before proceeding --->
<cfparam name="FORM.meal" default="" />
<!--- Convert the meal list to an array for looping --->
<cfset mealArray = listToArray(FORM.meal)>
<!--- Loop over the array values --->
<cfloop array="#mealArray#" index="currMeal">
<!--- Insert into database --->
<cfquery name="Add" datasource="food">
INSERT INTO Log (Name, Meal, Quantity)
VALUES (
<cfqueryparam value="#FORM.Name#" cfsqltype="cf_sql_varchar">
,<cfqueryparam value="#currMeal#" cfsqltype="cf_sql_varchar">
,<cfqueryparam value="#FORM['quantity_#currMeal#']#" cfsqltype="cf_sql_integer">
)
</cfquery>
</cfloop>
请记住,您应该考虑在事务中包装数据库插入,和/或考虑批量插入的方法。在循环内查询数据库不是最佳实践。
Please keep in mind that you should consider wrapping your database inserts in a transaction, and/or consider a method for bulk inserting. Querying a database inside a loop is not considered best practice.
这篇关于提交带有多个复选框/选择菜单的表单信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!