Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ColdFusion Form Data

Posted on 2011-03-15
5
Medium Priority
?
354 Views
Last Modified: 2012-05-11
Hi,
 I am very new to cold fusion but need to handle an issue. I have a form page in cold fusion, when we enter the data in the form and press submit button  it has to insert that data into the table in database.

Can anyone please please help me with this thing.
0
Comment
Question by:new_perl_user
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 39

Accepted Solution

by:
gdemaria earned 2000 total points
ID: 35139650
Here's a basic framework for a beginner-intermediate user.   Posting a form page to itself makes life easy and gives you some power.   The way it works is that you have a CFIF at the top of the page to process the action of the form submit.  But the first time the page loads, the form is not submitted, so this part is skipped.  The data is fetched from the able, copied into FORM.xxx scope variables and displayed in the form.  On submit, the same page is called, the action is processed.  If there is an error, it will display along with the form -with the same data the user entered - (not a refresh from the database).   If there is no error, then using CFLOCATION, you can go to any other page...

Take a look and ask questions, if you can get the hang of this is will be very usefull model for all pages...


<!--- this is the primary key for the data on this page, expect it to be passed, 
      if it is empty, the page will create a new record ----->
<cfparam name="form.contact_id" default="">  
		  
<cfset variables.error = "">  <!---- this variable will hold an error message, if a problem occurs ---->

<cfif IsDefined('form.submit')>  <!-- the page has been submitted, process action --->
  <cftransaction action="BEGIN"> <!--- a transaction ensure everything on page is saved together or rolledback on error ---->
  <cftry>  <!---- catch any errors in the cfcatch below ----->
   <cfloop index="ii" list="lastname,firstname,email">  <!--- validate required fields are not empty --->
     <cfif len(form[ii]) eq 0>
	    <cfthrow message="Please supply all required fields">
	 </cfif>
   </cfloop>
   <cfinclude template="owner_upd.cfm"> <!--- this file inserts or updates the data --->
   <cftransaction action="COMMIT">
  <cfcatch type="Any"> <!---- catch all errors, whether you cfthrow them or if happens in your sql statements ---->
     <cftransaction action="ROLLBACK">
     <cfset variables.error = cfcatch.message> <!---- set your error flag message variable ---->
  </cfcatch>
  </cftry>
  </cftransaction>
  <cfif len(variables.error) eq 0> <!--- if no error, go to your next page ---->
     <cflocation url="/nextpage.cfm?ID=AddVariablesHere" addtoken="no">
  </cfif>
</cfif>

<cfif len(variables.error) eq 0> <!--- this happens on first opening the file, get data ---->

   <cfquery name="getContact" datasource="#request.datasource#">
     select * from contacts
	 where contact_id = #val(form.contact_id)#
   </cfquery>
   <!---- the next line will copy every column from the query into a FORM
          variable.  This will allow you to use the form scope below instead
		  of the query name.  In case of an error, this allows your page
		  to reload the entered values without saving them ---->
   <cfloop index="ii" list="#getContact.columnList#">
      <cfset form[ii] = getContact[ii][getcontacts.currentRow]>
   </cfloop>
   
</cfif>

<cfif len(variables.error) gt 0> <!--- if an error, show it ---->
  <cfoutput><span style="color:red">#variables.error#</span></cfoutput>
</cfif>
<!--- here is your form to get the data ---->

<cfoutput>
<form method="post">  <!---- No Action in form, page will post to itself ---->

 First Name: <input type="Text" name="firstName" value="#form.firstName#"><br><br>
 
 Last Name: <input type="Text" name="LastName" value="#form.lastName#"><br><br>

 Email: <input type="Text" name="email" value="#form.email#"><br><br>

 <input type="Submit" name="submit" value="Save">

</form>
</cfoutput>

Open in new window

0
 
LVL 16

Expert Comment

by:Gurpreet Singh Randhawa
ID: 35147016
Wow! @gd Very Good and Great Way to handle the data insertion on single Page, Good Work!

Here is another Simple Technique!

No Points!

The Below code with small changes can be reused to the Update Statement as Well!




<cfparam name="firstname" default="">
<cfparam name="lastname" default="">
<cfparam name="email" default="">
<cftry>
<cfif StructKeyExists(FORM,'Save')>
	<cfif len(form.firstname) IS 0>
		<cfset s = "Error! Please Provide First Name">
	<cfelseif len(form.lastname) IS 0>	
		<cfset s = "Error! Please Provide Last Name">
	<cfelseif IsValid("email",form.email)>	
		<cfset s = "Error! Provide Valid Email Address">
	<cfelse>
		<cftransaction action="begin">
			<cftry>
				<cfquery datasource="test">
				insert into temp1(firstname,lastname,email) 
				values('#form.firstname#','#form.lastname#','#form.email#')
				</cfquery>
			<cftransaction action="COMMIT"/>
				<cfcatch type="Any"> 
				     <cftransaction action="ROLLBACK"/>
					     <cfset s = cfcatch.message> 
	  			</cfcatch>
			</cftry>
		</cftransaction>
	</cfif>	
</cfif>
<cfcatch>
	<cfset s = "Error! #cfcatch.Detail# #cfcatch.message#">
</cfcatch>
</cftry>
<cfoutput>
	<cfif isDefined('s')>
		<div align="left">#s#</div>
	</cfif>
<form method="post">  <!---- No Action in form, page will post to itself ---->
First Name: <input type="Text" name="firstName" value="#firstName#"><br><br>
Last Name: <input type="Text" name="LastName" value="#lastName#"><br><br>
Email: <input type="Text" name="email" value="#.email#"><br><br>
<input type="Submit" name="submit" value="Save">
</form>
</cfoutput>

Open in new window

0
 
LVL 16

Expert Comment

by:Gurpreet Singh Randhawa
ID: 35147023
change the line

Email: <input type="Text" name="email" value="#.email#"><br><br>

to

Email: <input type="Text" name="email" value="#email#"><br><br>
0
 
LVL 39

Assisted Solution

by:gdemaria
gdemaria earned 2000 total points
ID: 35147979
Thanks myselfrandhawa for pointing out that the insert/update code does not show (I included it using cfinclude).   The code would look the following.   It allows for insert or update of records depending on whether or not the primary key (ID) already exists..

myselfrandhawa, not sure why you would want to change the variable name from a nice, clear "error" to an  "s" ?   SidFishes, another expert on EE, calls those "Lazy Variables"  (love that!)   You are better off having distinctive names.  Also, imagine searching for the variable "S"... you would match TONS of letters through out your code making it very hard to find where "S" is actually used.


** Put this code in where I have <cfinclude template="owner_upd.cfm">  
** Or better yet create a file and put this code in it, so it can be shared
** The form page does BOTH add a record and Modify an existing record

             <cfif val(form.contact_id)> <!---- record exists, update it ---->
				<cfquery datasource="test">
				  update contacts
                    set firstname = <cfif len(form.firstname)>'#form.firstname#'<cfelse>NULL</cfif>
                       ,lastname = <cfif len(form.lastname)>'#form.lastname#'<cfelse>NULL</cfif>
                       ,email = <cfif len(form.email)>'#form.email#'<cfelse>NULL</cfif>
				  where contact_id = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#val(form.contact_id#">
				</cfquery>
             <cfelse> <!--- record doesn't exist, insert it ---->
				<cfquery datasource="test">
				insert into contacts (firstname,lastname,email) 
				values('#form.firstname#','#form.lastname#','#form.email#')
				</cfquery>
             </cfif>

Open in new window

0
 
LVL 16

Expert Comment

by:Gurpreet Singh Randhawa
ID: 35148238
Woo just my 2 cents I love those lazy variables :)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question