Step 3 Set Default Form Values ColdFusion Help

The next step is to set a series of variables for default form values. First you need four variables for the default values of the user-editable fields in the table: Last Name First Name Gender and Salary.

If the query result set the Record exists (and it will only when URL. Action was Insert Update or Load), then you want to use the values of the record in The Record to set your default form variables. If The Record doesn’t exist, then URL Action must have been Newand your default values should be the empty string because you want to create  an empty form.

In addition to these four default values, you need to decide whether a hidden field for the 10 number is necessary. A hidden field is necessary if you are updating a record but not if you are inserting a new one. On this basis you need to create-a fifth variable that contains the entire INPUT tag or the empty string as needed.

Finally a sixth field will provide the text to display in the main form’s submit button and a seventh field will provide the value to place in the URL.Action parameter of the URL for the form submission.

The code to set all these default values is a combinal’ion of CFIF, CFSETI and finally CFPARAMY ou use the Par ameter’Ext sts function to check whether The Record exists:

<CFIF IsDefinedC’TheRecord’»
<CFSET LastName z TheRecord.LastName>
<CFSET FirstName – TheRecord.FirstName>
<CFSET Gender – TheRecord.Gender>
<CFSET Salary – TheRecord.Salary>
<CFSET IDField – ‘<STRONG>Employee #TheRecord.ID#</STRONG><INPUT TYPE-HIDDEN
NAME-ID VALUE-#TheRecord.ID#>’>
<CFSET Submit – ‘UPDATE’>
<CFSET FormAction – ‘Update’>
</CFIF>
<CFPARAM NAME=’LastName’ ~EFAULT-“>
<CFPARAM NAME-‘FirstName’ DEFAULT-“>
<CFPARAM NAME2’Gender’ DEFAULT2”>
<CF?ARAM NAME2’Salary’ DEFAULT-“>
<CFPARAM NAME=’IDField’ DEFAULT-“>
<CFPARAM NAME=’Submit’ DEFAULT-‘INSERT’~
<CFPARAM NAME=’FormAction’ DEFAULT-‘Insert’>

Step 4: Display the Form

The final step is to display your form. Actually, there are two forms to display: One is a form prompting for the ID number of a record to update, and the second is a form for updating a record or inserting a new record. Finally, you need to display a link for users to indicate that they wish to insert a new record.

The first form for updating an existing record looks like this

<FORM METHOD=POST ACTION=’form.cfm?Action-Load’>
ID TO UPDATE: <INPUT TYPE-TEXT NAME-ID SIZE-3>
<INPUT TYPE=SUBMIT VALUE-‘LOAD’>
</FORM>

Notice the use of Action-Load as a parameter to the template. This example makes the assumption that your template is called form. cfm.

Next you need the editing and insertion form itself. This is based on the employee data form you have used throughout this chapter:

<CFOUTPUT>
<FORM METHOD-POST ACTION-form.cfm?Action-#FormAction#>
#IDField#
<TABLE BORDER-O CELLSPACING-S>

<CFOUTPUT>
<FORM METHOD-POST ACTION-form.cfm?Action-#FormAction#>
#IDField#
<TABLE BORDER-O CELLSPACING-S>

Finally, your link for the creation of new records is as follows

<A HREF=’form.cfm?Action=New’)Create a New Record</A>

FIGURE 15.11

FIGURE 15.11

If you enter data and click the Insert button, the new record will be created and then displayed in an update form like the one in Figure 15.12. Similarly, entering an employee 10 number in the 10 to Update field and clicking the Load button causes a record to be loaded and displayed, as shown in Figure 15.12

FIGURE 15.12

FIGURE 15.12

At this point you can change the data in the form and click the Update button to commit the changes to the database and then reload the record in a new update form.

Posted on November 17, 2015 in Forms

Share the Story

Back to Top
Share This