How to generate Dynamic Forms!
------------------------------

There are 2 ways to create and add forms the easy way, and the long way!

The long way entails setting out each form element and specifying everything, and then the update which can be long and just as tedious.

But, sometimes, there can be an easier way,
Here is the absolute quickest way

Form.cfm
---------
<html>
<body>

<!--- these are the names of the input boxes that will be laid out
these have to match column names in your database to work --->

<cfset address1="Firstname,Lastname,Phone,Address1,Address2,City,County,PostCode">


<!--- this is for when the form loops back on itself the insert is there to tell it the form is to be invoked if there is form.insert then we load add_address, a simple one line of code to update the database! --->

<cfif isdefined("form.insert")>
    <cfif form.insert eq
'insert'>
        <cfinclude template=
"add_address.cfm">
    </cfif>
</cfif>


<!--- create a table for our form to sit in --->
<table border=0 width=100%>
    <tr>
       <td colspan=2 bgcolor=silver>
<font face='verdana' size=2>Address</font></td>
    </tr>


    <cfoutput>

    <!--- make sure it loops back on itself --->
    <form action="index.cfm" method="post">

    <!--- this is the hidden input to let it know its been invoked --->
    <input type="hidden" name="insert" value="insert">

    <!--- now we cycle through the fields we've created --->
    <cfloop list="#address1#" index="i">
    <tr>
        <td>
<font face="arial" size=1>#i#</font></td>
        <td>
<input type="text" name="#i#" width="20"></td>
    </tr>

    </cfloop>

    </cfoutput>
</table>

<input type="submit" value="Continue >>">
</form>
</body>
</html>



now we add to the database!

Add_address.cfm
---------------

<cfinsert datasource="#application.dsn#" formfields="#address1#" tablename="address">

and thats it, the easiest, quickest way to add a form to a table!

About This Tutorial
Author: Alex Allen-Turl
Skill Level: Beginner 
 
 
 
Platforms Tested: CF5
Total Views: 38,788
Submission Date: December 24, 2002
Last Update Date: June 05, 2009
All Tutorials By This Autor: 7
Discuss This Tutorial
  • So it seems that with formFields Coldfusion automatically figures out that the list you gave it corresponds to the form on the page and populates the database? What if there's multiple forms?

  • Try to change datasource="#application.dsn#" to datasource="#what_ever_Your_database_Name_IS#"

  • INSERT INTO address (colums from address1) VALUES (values from address1)

  • I recieve this message how cn i fix? The error occurred in E:\Inetpub\campushare\web\Add_address.cfm: line 9 7 : 8 : 9 : 10 : 11 : and thats it, the easiest, quickest way to add a form to a table!

  • This is perfect what I am needing but the server I'm using doesn't support

Advertisement

Sponsored By...
Powered By...