Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

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

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.

EDIT 1:

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...