Skip to main content
Known Participant
April 26, 2013
Question

insert mutiple fields with same name

  • April 26, 2013
  • 1 reply
  • 500 views

I have the form with mutiple text boxes for user to enter email addresses.  It also has mutiple  checkboxes for user to select devision they want to add these email addresses to.

How can i loop the divNo and emails and insert them into table. The code i have below is worked fine for emails but i dont know how to do for divNo.  Can you please advice this?

<!---cfm--->

<form action="insert.cfm" method="post">

     <input type="checkbox" name="divNo" value="ACCT" /> ACCT <br>

     <input type="checkbox" name="divNo" value="FNA" /> FNA

     <br>

      <cfloop index="idx" from="1" to="5">

        <input type="text" name="email" size="30"><br>

    </cfloop>

</form>

!---insert.cfm--->

<cfloop list="#form.email#" index="x">

    <cfquery name="insert_email" datasource="#variables.dsn#">

        INSERT INTO [dbo].mytbl

          ([divNo]

          ,[email]

         )

VALUES

       (<cfqueryparam value="??" cfsqltype="cf_sql_integer" />,

        <cfqueryparam value="#x#" cfsqltype="cf_sql_varchar" />,

        )

            </cfquery>

</cfloop>

Thanks

This topic has been closed for replies.

1 reply

WolfShade
Legend
April 26, 2013

Since both checkboxes have the same name, any checked box value is submitted as a comma-delimited list, so in your case FORM.divNo will submit as ACCT,FNA if both checkboxes are checked.

In your insert.cfm, list loop through the FORM.divNo (if it exists - if neither is checked, FORM.divNo will not exist, so you should check for that, first.)  For each element in the list, run the insert query with the divNo value.  Since the email text inputs all have the same name, they are also being submit as a comma-delimited list.

^_^