Copy link to clipboard
Copied
I am trying to create a form that validates to make sure both fields have values entered.
If they have both values entered then go to preview page. If not then stay in the form page (main.cfm).
My below attempt is not working because sometimes it works with the validation and sometimes it doesnt work where it will send to preview page even if there are blank values or a blank value.
Please advise.
--------
main.cfm
<cfoutput>
<cfset myAction = "">
<cfif isDefined("form.submit")>
<cfif trim(form.city) eq "" or trim(form.state) eq "">
<cfset myAction = "main.cfm">
<cfelse>
<cfset myAction = "preview.cfm">
</cfif>
</cfif>
Both City and State are required.<br>
<form action="#myAction#" method="post">
City: <input type="text" name="city" value="<cfif isDefined("form.city")>#form.city#</cfif>"><br>
State: <input type="text" name="state" value="<cfif isDefined("form.state")>#form.state#</cfif>"><br>
<input type="submit" name="submit" value="Preview">
</form>
</cfoutput>
-----------
Preview.cfm
<cfoutput>
Preview of what you entered:<br>
City = #form.city#<br>
State = #form.state#
<form action="process.cfm" method="post">
<input type="hidden" name="city">
<input type="hidden" name="state">
<input type="submit" name="submit" value="Submit Information"> 
<input type="button" value="Go Back and Edit Form" onclick="history.go(-1)">
</form>
</cfoutput>
Copy link to clipboard
Copied
That logic shows a basic mis-understanding of server-client pograms and what goes on where.
Nothing is ever going to hapen in the desired order with that code. A form action clause is handled by the client browser. That is where it will submit the form request.
What is going to happen with that logic is something like this.
In the first itteration action will be blank. This will cause the form to submitt to the same URL it is on.
In the second itteration the action will be set to one or the other of the values, but the form will then just be displayed again because that is all the conditional code tells it to do.
In the third itteration the form will be submitted to the location determined by the logic ran in the second itteration.
------
There are many validation features for ColdFusion. A read of the documentation will provide insites into how to use them.
Copy link to clipboard
Copied
When reading the documentation, start with cfinput. It has a handy little attribute called required.
Copy link to clipboard
Copied
Thanks for your responses.
I tried required and it works with text input but not with cfselect in ColdFusion 8.
The cfselect doesnt validate a blank entry and lets the page go to the action page.
<cfform action="preview.cfm" method="post">
<cfselect name="city" required="yes" message="City is required field">
<option selected value = ""></option>
<option value = "sf">sf</option>
<option value = "ny">ny</option>
</cfselect>
....
Please advise.
Copy link to clipboard
Copied
Read the documentation carefully :
http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_r-s_14.html
(hint: your first option tag needs a small change to it's value).
Mack
Copy link to clipboard
Copied
just check <cfif form.city neq "">