3 Replies - 14557 Views - Last Post: 07 September 2010 - 06:10 AM Rate Topic: -----

#1 ProdicalProgrammer   User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 115
  • Joined: 26-June 09

Carrying over variables from one form to another

Posted 02 September 2010 - 12:20 AM

Hi Guys

How do you carry on the values from one form to another and then add it to a database.
Im really new at coldfusion.
Here are my two forms.

Information1
<cfparam name="FORM.FORMSUBMIT" default="">
<cfparam name="URL.fname" default="">
<cfparam name="URL.surname" default="">
<cfparam name="URL.email" default="">
<cfparam name="URL.cell" default="">
<html>
<head>
</head>
<body>
<table border="0" cellpadding="1" cellspacing="1">
<cfform action="information.cfm" method="get">
<td>
Your Details
</td>
<tr>
    <td width="25%" class="copy" align="left" valign="top">Name</td>
    <td width="75%" align="left">
    <cfinput type="text" name="fname" required="yes" message="Please enter a first name">
</tr>
<tr>
    <td width="25%" class="copy" align="left" valign="top">Surname</td>
    <td width="75%" align="left">
    <cfinput type="text" name="surname" required="yes" message="Please enter a surname">
</tr>
<tr>
    <td width="25%" class="copy" align="left" valign="top">Email</td>
    <td width="75%" align="left">
    <cfinput type="text" name="email" required="yes" message="Please enter an email">
</tr>
<tr>
    <td width="25%" class="copy" align="left" valign="top">Cell Number</td>
    <td width="75%" align="left">
    <cfinput type="text" name="cell" required="yes" message="Please enter a cell number">
</tr>
<tr>
	<td colspan="2" align="center">
	<a href="information2.cfm?ACTION=Get&fname=#URL.fname#&surname=#URL.surname#&email=#URL.email#&cell=#URL.cell#">Next</a>
    </td>
</tr>
</cfform>
</table>
</body>
</html>



Information2
<cfparam name="FORM.FORMSUBMIT" default="">
<cfparam name="URL.fname" default="">
<cfparam name="URL.surname" default="">
<cfparam name="URL.email" default="">
<cfparam name="URL.cell" default="">
<html>
<head>
</head>
<body>
<table border="0" cellpadding="1" cellspacing="1">
<cfform action="information.cfm" method="get">
<td>
Your Details
</td>
<tr>
    <td width="25%" class="copy" align="left" valign="top">Name</td>
    <td width="75%" align="left">
    <cfinput type="text" name="fname" required="yes" message="Please enter a first name">
</tr>
<tr>
    <td width="25%" class="copy" align="left" valign="top">Surname</td>
    <td width="75%" align="left">
    <cfinput type="text" name="surname" required="yes" message="Please enter a surname">
</tr>
<tr>
    <td width="25%" class="copy" align="left" valign="top">Email</td>
    <td width="75%" align="left">
    <cfinput type="text" name="email" required="yes" message="Please enter an email">
</tr>
<tr>
    <td width="25%" class="copy" align="left" valign="top">Cell Number</td>
    <td width="75%" align="left">
    <cfinput type="text" name="cell" required="yes" message="Please enter a cell number">
</tr>
<tr>
	<td colspan="2" align="center">
	<a href="information2.cfm?ACTION=Get&fname=#URL.fname#&surname=#URL.surname#&email=#URL.email#&cell=#URL.cell#">Next</a>
    </td>
</tr>
</cfform>
</table>
</body>
</html>



Im really new at this so if its not too much to ask could you explain my errors for me.
What happens is when i click on the next button in information1 nothing happens it just stays on that page.What i want it to do is take the information of Information1 and send it to Information2 which will then send it to a Database.

Thanks for your time.

Is This A Good Question/Topic? 0
  • +

Replies To: Carrying over variables from one form to another

#2 Craig328   User is offline

  • I make this look good
  • member icon

Reputation: 2042
  • View blog
  • Posts: 3,644
  • Joined: 13-January 08

Re: Carrying over variables from one form to another

Posted 02 September 2010 - 06:30 AM

Prodical:

Welcome to the world of Coldfusion!

Okay, so, what you're wanting is to have a form on one page (Information1) and submit the contents of that form to another page (Information2) for CF to save the submitted form contents to your database. The good news is that Information1 is nearly perfect for what you need (it requires only a couple of small changes) while Information2 will need an entire overhaul (not terribly difficult though as you'll see).

What you're currently doing wrong on the first code example (info1) is that you've set up a form pretty much properly...but then with line 37 you're not submitting it but calling a URL and trying to pass URL variables to that page. In fact, this CAN be done but it would require the use of a fair chunk of Javascript to do and the one thing you want to get accustomed to in dev work is to not over engineer a process when you don't need to. As it stands, you have a well constructed form and require only the replacement of the code you have on line 37 with a form submit:
<input type="submit" value="Next">

Now, what that will do is submit the form to the page specified as the action attribute in your cfform tag on line 11, information.cfm. As an aside, you need to change the method for that cfform tag from "get" to "post".

I don't know what the actual name of the page is that you're currently calling your Information1 sample...as long as it's not information.cfm. The solution I'm discussing envisions a form page (Information1) and a process page (Information2/information.cfm). If you wanted it all handled on one page (basically a self-submitting form/process page) that can be done too. So, let's build out information.cfm now so you can see what's going on.

Try this for starters:
<cfdump var="#form#">

In CF there are several different variable scopes available to work with. When you submit a form the page the form is submitted to will have the form fields submitted stored in a variables scope called, surprisingly enough, "form". If you're not familiar with the CFDUMP tag I'm introducing you to your new best friend. :) So, if you submit your form to information.cfm you should see a blue box containing two columns and several rows. You'll note that the left column contains the name of the variable from your form elements from your previous page and the right column contains the value the user put into those fields and submitted. This is a dump of the form scope and these variables (and others) are available for you to work with, manipulate and store.

So as to keep this relatively short (because we could get into a discussion of form validation and such...basically making sure you're getting the type of form info you're looking for) what you'd do now is take those form variables and stick them in your database and once that's done, perhaps redirect your user elsewhere. How do you put them into your database? You use a tag called CFQUERY. There are a couple of steps you'll need to have done first in order to use CFQUERY properly (setting up a datasource for your Coldfusion app to use via the Coldfusion Administrator) but assuming you do have a datasource set up, the rest from this point is relatively easy.

CFQUERY is the tag that allows you to interact with your database(s) so you can select/insert/update/delete data. This tag is one of the core "powers" of Coldfusion and is what allows it to work with dynamic (changing) data. You will need to know or learn SQL (structured query language) for the database product you're interacting with (SQL Server, MySQL, Oracle, etc). SQL is very similar across all database products but each has their own syntax and command quirks which you'll need to know. Anyway, sticking your data into a table in your database is a simply process and it would look something like this:
<cfquery name="testInsert" datasource="yourDatasourceName">
   INSERT INTO someTable (fname, surname, email, cell)
   VALUES (<cfqueryparam value="#form.fname#" cfsqltype="cf_sql_varchar">,
      <cfqueryparam value="#form.surname#" cfsqltype="cf_sql_varchar">,
      <cfqueryparam value="#form.email#" cfsqltype="cf_sql_varchar">,
      <cfqueryparam value="#form.cell#" cfsqltype="cf_sql_varchar">)
</cfquery>

That block of code above is all you need to put data into a database table in Coldfusion. From the form dump we did earlier (which you can now delete or comment out as you like so it doesn't show on the screen) we insert each of the form elements you collected from the previous form page (fname, surname, email and cell) and insert them into a table in your database. In the above example I named the various record fields in your table the same names you called the form input elements but they could be called whatever you like. One thing I added that is not MANDATORY but IS a good idea to get used to doing is I used CFQUERYPARAM tags to hold the submitted form variables for sending to your database. Without getting into too much detail, CFQUERYPARAM makes the queries go slightly faster as well as provides a barrier against some types of malicious user behavior (read: SQL injection attack attempts).

After that CFQUERY block has finished processing, your user is still on information.cfm so you can proceed to give them a little formatted message saying their form submission was successful or you can redirect back to their form page or pretty much whatever you want to do with them at that point.

This is a very simplistic example of how to collect form data and submit it to a Coldfusion processing page for insertion into your database. There are lots more things you can do to enhance security, validate submitted data, check for, catch and handle unexpected errors and so on. But this should get you started.

Good luck!

This post has been edited by Craig328: 02 September 2010 - 06:33 AM

Was This Post Helpful? 0
  • +
  • -

#3 ProdicalProgrammer   User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 115
  • Joined: 26-June 09

Re: Carrying over variables from one form to another

Posted 07 September 2010 - 03:13 AM

Hi Craig.

Thank you so much for your help man its much appreciated.

It really helped me.
Was This Post Helpful? 0
  • +
  • -

#4 Craig328   User is offline

  • I make this look good
  • member icon

Reputation: 2042
  • View blog
  • Posts: 3,644
  • Joined: 13-January 08

Re: Carrying over variables from one form to another

Posted 07 September 2010 - 06:10 AM

No problem. Glad to help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1