How to perform submit button function in ColdFusion?

10.1k views Asked by At

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

2

There are 2 answers

3
Adrian J. Moreno On BEST ANSWER

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>

0
Dan Bracuk On

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.