Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic form names 3

Status
Not open for further replies.

hpvic03

Technical User
Aug 2, 2006
89
Ok, so say I have a database with a table for group members, and another table for attributes for the group members. Each group member can have multiple attributes.

So I want to make a page with a form that has a text field for every group member for a specific attribute. I did this with a cfoutput query. But here's where I have trouble - what do I name each text field? I could name them something dynamic, like the group member's ID or name, but then how would I access each text field when I'm trying to save the information to the attribute table?

I can't name each field the same thing, because then it won't know which is which.

Any help would be greatly appreciated!
 
The way I'd go about this is create the form fields dynamically; something like:
Code:
<input type="text" value="" name="#memberID#_#GroupID#_1">
<input type="text" value="" name="#memberID#_#GroupID#_2">
<input type="text" value="" name="#memberID#_#GroupID#_3">
...
...
...

Then on the action page save the table accordingly. The _1 or _2 will indicate the field is different. You just scrub out that piece prior to saving it in the dB.

_____________________________
Just Imagine.
 
Ok the only problem I'm having with that is the SQL on the action page. For example, how would I do an update, because I can't do it like this:

<cfoutput query="members">

<cfquery datasource="#dsn#" name="update">
UPDATE tablename
SET attribute = '#form.#memberID#_#groupID#_3#'
WHERE memberID = '#memberID#'
</cfquery>
 
You can loop over the form variables to update. Something like:

Code:
<!--- LOOP OVER THE FORM FIELDS --->
<cfloop from="1" to="x" step="1" index="FileNumber">
	<!--- CHECK TO SEE IF THE FIELD IS NOT NULL --->
	<cfif form["#memberID#_#groupID#_" & FileNumber] NEQ "">
		<!--- UPDATE THE TABLE ONE BY ONE --->
		<cfquery name="" datasource="">
			UPDATE	tablename
			SET		attribute = '#memberID#_#groupID#_#FileNumber#'
			WHERE	memberID = <cfqueryparam cfsqltype="cf_sql_integer" value="#trim(memberID)#" maxlength="4" null="no">
		</cfquery>
	</cfif>
</cfloop>

_____________________________
Just Imagine.
 
so you don't have to specify the Form scope when setting the attribute field? i.e the "Form." before Form.formname?
 
You can name the attributes att1,att2 and att3 and have hidden inputs with the values of #memberID# and #GroupID# in each form. that way you can have an update statement like this:

UPDATE tablename
SET attribute='#FORM.att1#'
WHERE MemberID=#FORM.memberID#
 
that last one will not work. the idea is to have one submit button to submit all the text fields - so I can only have one form. So #form.memberID# would be different for every text field, and thus unaccessible.
 
Not true... what I do when I want to process a bunch of records with one submit, I name the field according to its ID and then loop through a list with an Evaluate() statement like this...

Code:
<!--- FORM --->
<cfform action="action.cfm?update=yes" method="post">
<table>
<cfoutput query="qMembers">
 <tr>
  <td>
    <!--- ATTRIBUTE --->
    <cfinput type="text" name="a#MemberID#" />
    <!--- Member ID --->
    <input type="hidden" name="ID" value="#MemberID#" />
    <!--- GROUP ID --->
    <input type="hidden" name="GroupID" value="#GroupID#" />
  </td>
 </tr>
</cfoutput>
 <tr>
  <td colspan="3">
   <input type="submit" name="btnGo" value="go" />
  </td>
 </tr>
<table>
</cfform>

<!--- PROCESSING --->
<cfif isDefined("url.update")>
 <cfloop index="i" List="#Form.ID#">
  <cfset ID=#Evaluate("#i#")#>
  <cfif IsDefined("ID")>
   <cfoutput>
     <cfset myAtt=#Evaluate("a" & "#ID#")#>

      <cfquery name="qUpdate" datasource="myDb">
        UPDATE myTable
        SET myAtt = #myAtt#
        WHERE (MemberID = #ID#)
      </cfquery>				
    </cfoutput>
   </cfif>
 </cfloop>
 <h3 style="font-family:Arial;" align="center">Records  Updated!</h3>
</cfif>

I promise it works. I use it to update
 
hpvic03,

Assuming your group members attribute table has a unique id (which it should) this is very simple.

In your cfoutput, use #CurrentRow# to name the fields sequentially: field1, field2, field3, ... etc

Code:
--- psuedo code ---
<form ..>
   <cfoutput query="yourQuery">
     <input name="attribute#CurrentRow#" 
          type="text" 
          value="#attributeColumn#">
     <input name="uniqueID#CurrentRow#" 
          type="hidden" 
          value="#uniqueIDColumn#">
   </cfoutput>
   <!--- save number of rows in hidden field --->
   <cfoutput>
     <input type="hidden" 
            name="numOfRows" 
            value="#yourQuery.recordCount#">
   </cfoutput>
</form>

On the action page, loop from 1 to #form.numOfRows# and extract the form field values, using the uniqueID to update each record.

Code:
--- psuedo code ---
<cfloop from="1" to="#form.numOfRows#" index="i">
   <cfset attributeValue = form["attribute"& i]>
   <cfset uniqueIDValue = form["uniqueID"& i]>
   
   <cfquery datasource="#dsn#" name="update">
      UPDATE tablename
      SET attribute = <cfqueryparam value="#attributeValue#" 
                      cfsqltype="your column type">
      WHERE memberID = <cfqueryparam value="#uniqueIDValue#" 
                       cfsqltype="your column type">
   </cfquery>
</cfloop>
 
Thanks everbody for your help! You are what makes this forum so great!!

So here's what I ended up learning/using:

Using form with brackets will allow me to use the form scope, but with a variable inside.

So on my action page I looped through my form items then did

<cfif form[uniqueID] neq "">

..sql action..

</cfif>

And it was easiest for me to name my form items by their unique ID's, although everyone else's methods would have worked.

Thanks again!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top