Creating a dynamic form, but how to do the query?

前端 未结 3 1726
余生分开走
余生分开走 2020-12-21 21:34

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 ma

相关标签:
3条回答
  • 2020-12-21 22:16

    Uses a static number loop but breaks once a number in the sequence hasn't been passed. You could also count the number fields in jQuery and pass it along with the form submission

    <cfquery datasource="exampledatasource" name="insertbandmembers">
      INSERT Into members(members_name)
      VALUES
      <cfloop index="i" from="1" to="15">
        <cfif structKeyExists(form, 'memberName#i#') AND len(form['memberName#i#'])>
          <cfif i neq 1>, </cfif>
          ( <cfqueryparam cfsqltype="cf_sql_varchar" value="#form['memberName#i#']#"> )
        <cfelse>
          <cfbreak> 
        </cfif> 
      </cfloop>
    </cfquery>
    
    0 讨论(0)
  • 2020-12-21 22:22

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

    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.

    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>
    
    0 讨论(0)
  • 2020-12-21 22:24

    I would do something like this:

    <cfloop list="#form.fieldnames#" index="ThisElement">
        <cfif left(ThisElement, 10) is "memberName">
           <cfset ThisValue = form[ThisElement]>
    
           <cfif len(ThisValue)>
               <cfquery name="AddRecord">
                  Insert Into members(members_name)
                  Values 
                  ( <cfqueryparam cfsqltype="cf_sql_varchar" value="#ThisValue#"> )
               </cfquery>
           </cfif>  <!--- len(thisvalue) --->
    
        </cfif>  <!--- left(ThisElement, 10) is "memberName" --->
    </cfloop>
    
    0 讨论(0)
提交回复
热议问题