创建动态表单,但如何进行查询? [英] Creating a dynamic form, but how to do the query?

查看:27
本文介绍了创建动态表单,但如何进行查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个动态表单,可让您修改所需输入字段的数量.这是因为他们可能有 15 个乐队成员并且需要 15 个字段,或者一个乐队可能有 2 个.我唯一的问题是提交表单时,每个输入都命名为memberName1"、membername2"等.完成使用 jQuery.我将如何在运行查询时/之前运行检查以查看这些字段是否已填充以及它们是否要将它们的值输入到数据库中?谢谢.

I've created a dynamic form which allows you to modify the amount of input fields you need. This is because they may have 15 band members and need 15 fields, or one band may have 2. The only I problem I have is when submitting the form, each input is named as "memberName1", "membername2" etc. which is done using jQuery. How would I run a check while/before running the query to see if those fields are filled and if they are to input their values into the database? Thanks.

编辑 1:

我在想类似下面的代码.但我需要运行一个循环来计算名称为 memberName 的所有当前字段,后跟一个数字.与其在第一个空旷的地方停下来,不如把它们都跑一遍.如果值为空白,则什么也不做.如果它有一个值,则将其插入我的表中.

I was thinking something like the code below. But I would need to run a loop to count through all the present fields where the name is memberName, followed by a number. Rather than stopping at the first empty field, run through them all. If the value is blank do nothing. If it's got a value then insert it into my table.

<cfquery datasource="exampledatasource" name="insertbandmembers">
  Insert Into members(members_name)
  Values (memberName#i#)
</cfquery>

推荐答案

(这开始是评论,但是太长了..)

有不同的方法来处理它.我的偏好是 Travis 中提到的那个评论.基本上让 jquery 将成员总数存储在隐藏的表单字段中.然后使用该值循环访问成员字段、验证值并根据需要插入.

There are different ways to approach it. My preference is the one Travis mentioned in the comments. Basically have jquery store the total number of members in a hidden form field. Then use that value to loop through the member fields, validate the values, and insert as needed.

您可以轻松适应 Danjamckinn 的例子.注意到他们正在使用 <cfqueryparam> 来帮助防范 sql 注入?此外,由于这些是相关的插入,您希望将整个内容包装在单个 <cftransaction> 中.这确保了插入物被视为一个单元.他们要么都成功,要么都失败——一起.

You could easily adapt Dan or jamckinn's example to do this. Notice they are using <cfqueryparam> to help guard against sql injection? Also, since these are related inserts you want to wrap the whole thing inside a single <cftransaction>. That ensures the inserts are treated a single unit. Either they all succeed or they all fail - together.

    <cfparam name="form.totalFields" default="0" >

    <cfloop from="1" to="#val(form.totalFields)#" index="x">
        <!--- extract current name --->
        <cfset memberName = trim(FORM["memberName"& x])>

        <!--- insert NON-empty values --->
        <cfif len(memberName)>
            <cfquery datasource="exampledatasource" name="insertbandmembers">
                INSERT INTO members ( members_name )
                VALUES 
                ( 
                   <cfqueryparam value="#memberName#" cfsqltype="cf_sql_varchar"> 
                )
            </cfquery>
        </cfif>
   </cfloop>

这篇关于创建动态表单,但如何进行查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆