3

I am new to coldfusion , please check my code below

<cfif isDefined("form.submit")> 
 <cfoutput> 
     <h3>hi</h3> 
 </cfoutput> 
</cfif> 
<cfform action="#CGI.SCRIPT_NAME#"> 
  User Name:<cfinput type="Text" name="usr_nm"><br> 
  <cfinput type="Radio" name="access_flg" value="0">Admin
  <cfinput type="Radio" name="access_flg" value="1">User</br>
  <cfinput type="submit" name="submit" value="submit"><br> 
</cfform>

But ,When I am clicking submit button ,I am expecting result as hi

I haven't see hi message, Is there any thing wrong in my code ,Any one please help me

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
Soujanya
  • 277
  • 4
  • 17
  • I just ran this exact code on my own page (CF11) and it executed as you expected. Logic-wise this code is perfectly valid as I can tell. – TRose Feb 11 '16 at 16:48
  • Thanks for the replay..,My requirement is like this... when end user click submit button i want to get details from textbox and radio button ,then insert into database ,I don't want to redirect another page,How can I do this ..could you please help me – Soujanya Feb 11 '16 at 16:51
  • 2
    Try adding method="post" to your CFFORM tag. In older versions of CF, it defaulted to "get," which'd cause the behavior you describe. – Joe Rinehart Feb 11 '16 at 17:03
  • Thanks for the reply ,I am trying to execute my code in online http://trycf.com/ ,I haven't see hi – Soujanya Feb 11 '16 at 17:08
  • 2
    Did you follow Joe's suggestion? By the way, not related to your question, but `` has advantages over ` `. – Dan Bracuk Feb 11 '16 at 17:13
  • yes..i added metod=post..but no luck. – Soujanya Feb 11 '16 at 17:17
  • 1
    @Soujanya do you have anything inside an htaccess or web.config that re-writes URLs? – TRose Feb 11 '16 at 17:20
  • 2
    *execute my code in online trycf.com* .. which could explain it. Not everything will work *exactly* the same as if you were running the code locally. Keep in mind those pages are dynamic, so a self-posting form may not work the same way (or at all). – Leigh Feb 11 '16 at 18:12
  • 2
    You do not gain anything from using `cfform` in this case. Use a plain old `form` and `input` elements and, as Joe noted, add `method="post"`. And...run the code in a local version of ColdFusion, not on trycf.com. – Scott Stroz Feb 11 '16 at 18:13
  • I second what @ScottStroz says. There's no reason to use `` here (or, in general: ever). Just use a `
    `
    – Adam Cameron Feb 11 '16 at 19:08

2 Answers2

5

Since you're new to ColdFusion, I'll give you some advice straight away:

1. Do not submit a form to the same page.

Submit the form to a separate page for processing. Reason being, as you get into more advanced applications, you'll need to restrict pages/URLs to only respond to an appropriate HTML Verb.

Your form page should respond to HTTP GET. Your form processing page should only respond to HTTP POST.

2. Do not use CFFORM.

The function of CFFORM is to create JavaScript validation and server-side interactions. This can easily be done with modern JavaScript libraries like

3. Give your form elements an ID, as well as a NAME.

This allows easier reference to the form elements when using JavaScript.

4. Do not name your submit button "submit".

If you ever want to use JavaScript to submit a form, the function is submit().

For example: $('#myForm').submit();

Having a form element named the same as a function will cause errors.

Here's my_form.cfm:

<form id="myForm" name="myForm" action="my_form_action.cfm" method="post">
    User Name:<input type="Text" id="usr_nm" name="usr_nm"><br> 
    <input type="Radio" id="access_flg_0" name="access_flg" value="0">Admin
    <input type="Radio" id="access_flg_1" name="access_flg" value="1">User</br>
    <input type="submit" id="my_form_submit" name="my_form_submit" value="Submit"><br> 
</form>

5. You don't need to use CFOUTPUT unless you are rendering data from the server.

Here's my_form_action.cfm:

<cfif structKeyExists(form, "my_form_submit")> 
<h3>Hi!<lt>
</cfif>

Even better:

<cfif (cgi.request_method IS "post") AND (structKeyExists(form, "my_form_submit"))> 
<h3>Hi!<lt>
</cfif>

Adrian J. Moreno
  • 14,350
  • 1
  • 37
  • 44
  • All good tips. @Soujanya - Putting aside best practices for a moment, there is nothing *technically* wrong with the code. A self posting form simply won't work on that site, due to the way the dynamically generated scripts are handled. Online tools are a great convenience, but .. they (understandably) have some limitations. What you experienced is one of them. I recommend trying one of the CF express versions (both Adobe and Lucee have one). Just download and click a .bat file and you are ready to run .cfm scripts on your local machine (and try out the tips above :-) – Leigh Feb 11 '16 at 20:38
  • Thanks for the suggestions , I followed "Adrian J. Moreno " ,That is working fine.After completed my operation in action class.I want to come back to the original page(means HTML) page,How can I do this ,could you give me some suggestions – Soujanya Feb 12 '16 at 15:28
  • @Soujanya the easiest thing to do is to use `CFLOCATION` to redirect from the processing page to whichever page you want to go next. – Adrian J. Moreno Feb 12 '16 at 18:44
1

This is an elaboration of this part of Adrian's answer:

<cfif (cgi.request_method IS "post") AND (structKeyExists   form, "my_form_submit"))> 
<h3>Hi!</h3>
</cfif>

This is a candidate for code re-use. In one of our applications, I wrote a custom tag that does something like this:

if (StructKeyExists(attributes, 'ScopeToCheck') is false)
attributes.ScopeToCheck = "form";

if (StructKeyExists(caller, attributes.ScopeToCheck) is false) 
Redirect = true;
else if (StructIsEmpty(caller[attributes.ScopeToCheck]) is true)
Redirect = true;
else
Redirect = false;
if (Redirect == true)
location(somewhere, false);

The custom tag approach was appropriate for my situation. For other situations, the same logic can be put into a udf that returns either true or false. Then the calling page can decide what to do with that information.

Dan Bracuk
  • 20,699
  • 4
  • 26
  • 43