Skip to main content
Participating Frequently
October 29, 2010
Answered

Newbie question - passing variables (aka why does this work?)

  • October 29, 2010
  • 2 replies
  • 1929 views

I spent a few hours unsuccessfully trying to pass the value of an html text box to a 2nd coldfusion page via the URL (query string).

However, it seems I don't have to do that because the 2nd page seems to be able to reference the text box value anyways.  My question is: Is this a fluke or is this the normal way things work?  When I click the "Run Report" button, whatever I typed in the text box shows up on the screen from page2.cfm.  I thought that I would have to pass this value somehow (via URL string or session variables or something).

So, my newbie question is: Is this the way it should work (a simple Yes or No answer is fine)A text box from one .CFM file can reference the text box value from another .CFM page? Thanks in advance

Here's page 1 (page1.cfm):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>

<CFform action="http://www.mywebsite.com/page2.cfm" method="get">
Cust ID: <cfinput type="text" name="custid">
<input type=submit value="Run Report">

</CFFORM>
</body>
</html>

Here's page 2(page2.cfm) which works fine and displays whatever I typed in the text box:

<CFOUTPUT>
#CUSTID#
</CFOUTPUT>

This topic has been closed for replies.
Correct answer -__cfSearching__-

I was already typing this before Ian responded. But I may as well post it in case it helps ...

> I spent a few hours *unsuccessfully* trying to pass the
> value of an html text box to a 2nd coldfusion page via the
> URL (query string).

Whether you know it or it, that is actually what you are doing.

> <CFform action="http://www.mywebsite.com/page2.cfm" method="get">
> Cust ID: <cfinput type="text" name="custid">

When you use method="get", the form field variables are passed in the query string. So if you type "abc" into the text box, the submitted URL will be:

http://www.mywebsite.com/page2.cfm?custid=abc

So "custid" will be available as a variable in the URL scope on page2.

> *Here's page 2(page2.cfm) which works fine and displays
> whatever I typed in the text box:*
> <CFOUTPUT>
> #CUSTID#
> </CFOUTPUT>

The reason it works is a bit of CF magic. Since the #custID# variable is un-scoped, CF searches for that variable in a predefined list of scopes (see link below). It searches each one and uses the first value it finds. Though not the exact search order, CF will check the

http://livedocs.adobe.com/coldfusion/8/Variables_32.html
- VARIABLES scope (then ..)
- URL scope (then ..)
- FORM scope (then ..)
...etc...

In your case the variable exists in the URL scope. So your code is equivalent to saying

        <cfoutput>#url.CUSTID#</cfoutput>

That is actually a better coding practice. Though the original code works without the variable scope, providing an explicit scope is more efficient and helps prevent unitentional errors.  For example if the page2 code was:

      <cfset custID = "Which value will CF use?">
     <CFOUTPUT>#CUSTID#</CFOUTPUT>

... the output might not be what you would expect ;-)

So to answer the question: yes that is what should happen.  However, you should explicity scope your variables on page 2 ie use #url.custID#

2 replies

-__cfSearching__-Correct answer
Inspiring
October 29, 2010

I was already typing this before Ian responded. But I may as well post it in case it helps ...

> I spent a few hours *unsuccessfully* trying to pass the
> value of an html text box to a 2nd coldfusion page via the
> URL (query string).

Whether you know it or it, that is actually what you are doing.

> <CFform action="http://www.mywebsite.com/page2.cfm" method="get">
> Cust ID: <cfinput type="text" name="custid">

When you use method="get", the form field variables are passed in the query string. So if you type "abc" into the text box, the submitted URL will be:

http://www.mywebsite.com/page2.cfm?custid=abc

So "custid" will be available as a variable in the URL scope on page2.

> *Here's page 2(page2.cfm) which works fine and displays
> whatever I typed in the text box:*
> <CFOUTPUT>
> #CUSTID#
> </CFOUTPUT>

The reason it works is a bit of CF magic. Since the #custID# variable is un-scoped, CF searches for that variable in a predefined list of scopes (see link below). It searches each one and uses the first value it finds. Though not the exact search order, CF will check the

http://livedocs.adobe.com/coldfusion/8/Variables_32.html
- VARIABLES scope (then ..)
- URL scope (then ..)
- FORM scope (then ..)
...etc...

In your case the variable exists in the URL scope. So your code is equivalent to saying

        <cfoutput>#url.CUSTID#</cfoutput>

That is actually a better coding practice. Though the original code works without the variable scope, providing an explicit scope is more efficient and helps prevent unitentional errors.  For example if the page2 code was:

      <cfset custID = "Which value will CF use?">
     <CFOUTPUT>#CUSTID#</CFOUTPUT>

... the output might not be what you would expect ;-)

So to answer the question: yes that is what should happen.  However, you should explicity scope your variables on page 2 ie use #url.custID#

vinzinAuthor
Participating Frequently
October 29, 2010

hi ilssac and cfsearching,

   Thanks for your quick and detailed replies -- I appreciate it.  I do now see that the query string for the 2nd page contains the form values, and I also replaced the CF tags with normal HTML and the result was the same (as ilssac noted).

    I do need a better understanding of basic HTML and CF, but this little issue was holding me back.  My original problem was that I was trying to pass "form" values via the <a> tag and href attribute.

    Like most people, I learn best by doing -- so I'll be able to continue now with a better understanding of things.

    Thanks again for your clear and informative explanations.

                          Regards,

                            vince

p.s. sorry if I screwed up on the answer buttons.  I meant to give you both "correct answer" votes, but I think I gave one of you only a "helpful" vote.

ilssac
Inspiring
October 29, 2010

vinzin wrote:

I do need a better understanding of basic HTML and CF, but this little issue was holding me back.  My original problem was that I was trying to pass "form" values via the <a> tag and href attribute.

I am not sure what you mean by "'form' values via the <a> tag".  But you can easily pass URL values just like the <form...method="get"> does.  I.E.  <a href="page2.cfm?custid=foobar">Foobar on page two</a>. would pretty much end up with the same results as the above form.

vinzin wrote:

p.s. ... I meant to give you both "correct answer" votes

That is not allowed on this forum.  You can only assign one 'correct' answer and two helpful answers for each topic.

ilssac
Inspiring
October 29, 2010

YES it is the way that it works.

But it works with or without ColdFusion.  You will save yourself a lot of problems and misunderstanding if you get a good basis of what is basic HTTP functionality so that you don't confuse that with CFML functionality.

You happend to be using <cfform...> and associated <cfinput...> tags in your code.  But you are not using any of the features for those tags, so your code could easily be replaced by plain HTML <form...> and <input...> tags.  Because that is all ColdFusion is doing, replacing them and sending the resulting HTML content to the browser.

The normal HTTP functionality with forms is that the form fields will be included in the URL (aka get) or form-headers (aka post).

The only thing that is 'automatically' happening here is that ColdFusion will automatically take those get and post key-value pairs and create the URL and FORM data structures with the information.  Thus it is very easy to access this information in the CFML page requested by the form action="..." parameter.

So back to the beginning, Yes this is the way it should work.

Inspiring
October 29, 2010

You will save yourself a lot of problems and misunderstanding if you get a good basis of what is basic HTTP functionality so that you don't confuse that with CFML functionality.


Which is a good point.  The fact that the "custID" value is passed in query string has nothing to do with CF. It is basic http behavior whenever you use any html <form> with method="get" 

The only thing that is 'automatically' happening here is that ColdFusion will automatically take those get and post key-value pairs and create the URL and FORM data structures with the information.

Yep. That part of the behavior is specific to CF.