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

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

问题描述

我创建了一个动态表单,允许您修改所需的输入字段数量。这是因为他们可能有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>


推荐答案

,但太长了..)

(This started out to be a comment, but it is too long .. )

有不同的方法来处理它。我的首选项是 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.

您可以轻松调整 Dan jamckinn 的示例来执行此操作。注意,他们使用< 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天全站免登陆