Self Submitting Form

I am trying to create a self submitting form. I have something basic right now just to see if it works. What i am trying to do is to get the cfoutput to change when the form is submitted. When i try the form i don’t get any errors and the cfoutput does not change. I am new to Cold Fusion any help is appreciated. Thanks



<cfif NOT isDefined("form.submit")>
	<cfset #FormSumit# = "Form Not Submitted">
<cfelse>
    <cfset #FormSumit# = "Form Submitted">
</cfif>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Form Submission</title>
</head>

<body>
<cfoutput>#FormSumit#</cfoutput>


<cfform method="post">
<cfinput type="text" name="FirstName" >
<cfinput type="text" name="LastName" >
<cfinput type="submit" name="submit">


</cfform>
</body>
</html>


That will never work if you think about it, the form is not submitted by default right? and the variable FormSumit doesn’t exist on page load. So you should first set the variable before you can do anything with it:


<!--- Declare Variable FormSumit --->
<cfparam name="FormSumit" type="string" default="Form Not Submitted" />

<cfif isDefined("form.submit")>
    <!--- You don't need all these # signs --->
    <cfset FormSumit = "Form Submitted">
</cfif>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<cfoutput>#FormSumit#</cfoutput>
<cfform method="post">
<cfinput type="text" name="FirstName" >
<cfinput type="text" name="LastName" >
<cfinput type="submit" name="submit">
</cfform>
</body>
</html>

This should do

I’m not an expert but would this work?



<cfif isdefined('form.submit')>
 <p>Your form was submitted !</p>
</cfif>

<form method="post" action="<cfoutput>#CGI.SCRIPT_NAME#</cfoutput>">
<label for="Fname">First Name</label><input type="text" name="Fname"><br />
<label for="Fname">Surname Name</label><input type="text" name="Sname"><br />
<input type="submit" name="Submit"><br />
</form>

Just on a side note :slight_smile:

Don’t use isDefined(), use structKeyExists() instead.

isDefined() is a bottleneck. You’re passing a string to ColdFusion which then has to pull it apart and then systematically work out from your syntax where that variable sits.

Systematically going through scopes is dangerous. Why? Because if I declare a variable like this …

<cfset variables.form.submit = “I’m in the variables scope not form” />

CF will find that before it goes anywhere near the form scope.

Using structKeyExists is good for a few reasons

Doing this is easier to read and better documentation within your code…

<cfif structKeyExists(form,‘submit’) >

It also tells ColdFusion straight away what scope to look in and what the variable is called, so it’s a lot quicker.

Stating where the variables sit in your application also makes your code more stable as you’re now managing the scopes yourself rather than getting ColdFusion to do guess work :slight_smile:


Final tip - both of these functions are asking an “unknown”. In other words you don’t know if that variable exists or not. So use parameters in your code. i.e.

<cfparam name=“form.submit” type=“string” default=“” />

Stating the variables at the top of your page is documentation for Developers and if they don’t exist that the default is blank.

If an actual variable does exist it will use that value, for the cases where it doesn’t you’ve still got it but it’s blank. So your code can now read like this:


<cfparam name="form.submit" type="string" default="" />

<!--- If form.submit has any value inside it run this code... --->
<cfif len(form.submit)>

</cfif>

Hope that helps.

Cheers,
James