<cfif> not working. Pre-check values on add form being ignored

jslaught
jslaught used Ask the Experts™
on
I guess i'm becoming a regular on here with asking questions but I do really love this site and appreciate all the expert help.

I have a coldfusion add new record form and would like to check the *required values before allowing the submission of the new record. The cfif that I have in the form is ignoring the checks. Below are the code for both forms.
<!--- Add New Record form ---
================================ --->

<!--- query to put a list of of Request options in a dropdown box during an insert --->
<cfquery name="dropdownvalues" datasource="sspsrs">
    SELECT Title_Name
    FROM IssueTitles order by Title_Name
</cfquery>

<!--- query to put a list of of SSP Internal network names from the database in a dropdown box during an insert --->
<cfquery name="ddvsnetwork" datasource="sspsrs">
    SELECT Network
    FROM Network
</cfquery>

<cfquery name="clas" datasource="sspsrs">
    SELECT Classification
    FROM Classification
</cfquery>


<!--- query to put a list of "Full_names" names from the database in a dropdown box during an insert --->
<cfquery name="fname" datasource="sspsrs">
    SELECT Full_name
    FROM tblAdmins order by full_name
</cfquery>

<!--- query to put a list of customer "Last Names" from the database in a dropdown box during an insert --->
<cfquery name="ln" datasource="sspsrs">
    SELECT distinct Last_Name
    FROM Customers order by last_name
</cfquery>

<!--- query to put a list of customer "First Names" from the database in a dropdown box during an insert --->
<cfquery name="fn" datasource="sspsrs">
    SELECT distinct First_Name
    FROM Customers order by first_name
</cfquery>

<cfquery name="rstatus" datasource="sspsrs">
    SELECT status
    FROM Status
</cfquery> 

<cfquery name="pr" datasource="sspsrs">
    SELECT priority
    FROM priority
</cfquery>


    <cfoutput><a href="trackertabs.cfm?user_name=#url.user_name#&tab=2"></cfoutput>    
    <img src="http://127.0.0.1:8500/TicketTracking/pics/returnwc.jpg" width=60 height=60 alt="go back to service tab" border="0" /></a>

<body bgcolor="#99ccff">
	<table width="100%" border="0">
<br>
<font size = 4><b><u>Add A New Service Request</u></font>
<br>
<br>
==========<Br>
* Required<br>
==========<br>
<cfoutput>

<cfform action="http://127.0.0.1:8500/TicketTracking/insertnewsr.cfm?user_name=#url.user_name#" method="Post">
  			
			<tr>
				  <td><br><b>* Request Type:</b></br></td>
				  <td><select name="Title">
                                  <option value="">---Select Option---</option>
				  <cfloop query="dropdownvalues">
				  <option value="#dropdownvalues.title_name#">#dropdownvalues.title_name#</option>
				  </cfloop>
				  </select></td>
				  
			
			</tr>
			<tr>
				<td><br><b>* Opened Date:</b></br></td>
				<td><cfinput name="Opened_Date" type="datefield" validate="date" monthnames="Jan,Feb,March,April,May,June,July,August,September,October,November,December"></td>
			</tr>
			
  			<tr>
				<td><br><b>* Network:</b></br></td>
				  <td><select name="Network">
				  <option value="">---Select Option---</option>
				  <cfloop query="ddvsnetwork">
				  <option value="#ddvsnetwork.network#">#ddvsnetwork.network#</option>
				  </cfloop>
				  </select></td>
			<tr>
				<td><br><b>* Classification <br>of Request:</b></br></td>
				  <td><select name="classification_of_Request">
				  <option value="">---Select Option---</option>		
				  <cfloop query="clas">
				  <option value="#clas.classification#">#clas.classification#</option>
				  </cfloop>
				  </select></td>
  			</tr>
  			<tr>
  				
				  <td><br><b>* Assigned To:</b></br></td>
				  <td><select name="Full_Name">
				  <option value="">---Select Option---</option>
				  <cfloop query="fname">
				  <option value="#fname.Full_Name#">#fname.Full_name#</option>
				  </cfloop>
				  </select></td>
  			</tr> 
  			
  			<tr>      <td><br><b>* Customer <br> Last Name:</b></br></td>
				  <td><select name="Last_Name">
				  <option value="">---Select Option---</option>
				  <cfloop query="ln">
				  <option value="#ln.Last_Name#">#ln.last_name#</option>
				  </cfloop>
				  </select></td>
  			</tr> 
  			<tr>
				  <td><br><b>* Customer <br> First Name:</b></br></td>
				  <td><select name="First_Name">
				  <option value="">---Select Option---</option>
				  <cfloop query="fn">
				  <option value="#fn.First_Name#">#fn.first_name#</option>
				  </cfloop>
				  </select></td>
  			</tr>
  			
			<tr>
				<td><br><b>Closed Date:</b></br></td>
				<td><cfinput name="Closed_Date" type="datefield" validate="date" monthnames="Jan,Feb,March,April,May,June,July,August,September,October,November,December"></td>
			</tr>
  			<tr>
				 <td><br><b>* Status:</b></br></td>
				  <td><select name="Status">
				  <option value="">---Select Option---</option>
				  <cfloop query="rstatus">
				  <option value="#rstatus.status#">#rstatus.status#</option>
				  </cfloop>
				  </select></td>
  			</tr>
			<tr>
				  <td><br><b>* Priority:</b></br></td>
				  <td><select name="priority">
				  <option value="">---Select Option---</option>
				  <cfloop query="pr">
				  <option value="#pr.priority#">#pr.priority#</option>
				  </cfloop>
				  </select></td>
  			</tr>
			<tr>
				<td><br><b>Description:</b></br></td>
				<td><font size=2><textarea name="description" rows="5" cols="40"></textarea></td>
  			</tr>
			
			<tr>
				<td><br><b>Comments:</b></br></td>
				<td><font size=2><textarea name="comments" rows="5" cols="40"></textarea></td>
  			</tr> 

			<tr>
				<td> <br></td>
				<td><cfinput type = "submit" name="insert" class="button" value = "Add New Record"  
									onClick="return confirm('Are You Sure You Want To Add This Record?');"></br></td>
			</tr>
		</cfform>
		</cfoutput>
</table>
</body>

<!--- ============ INSERT RECORD FORM ====
-------------------------------------------->


<!---<cfdump var="#FORM#" label="FORM variables">--->

<cfquery name="addun" datasource="sspsrs">
    SELECT user_name
    FROM tblAdmins where Full_name = <cfqueryparam value="#form.full_name#" cfsqltype="cf_sql_varchar"> 
</cfquery>

<cfif #form.status# is 'Resolved' and #form.closed_date# is "">
<script>
              alert("Please choose a closed date before closing this service request.");
              self.location="http://127.0.0.1:8500/TicketTracking/addnewsr.cfm?<cfoutput>user_name=#url.user_name#</cfoutput>";
</script>

<!--- THIS IS WHERE I'M HAVING THE PROBLEM --->

<cfif #form.title# is "">
<script>
              alert("You must select a Request Type!");
              self.location="http://127.0.0.1:8500/TicketTracking/addnewsr.cfm?user_name=#url.user_name#";
</script>


<cfelse>



<CFQUERY NAME="insert" DATASOURCE="sspsrs">
INSERT INTO Issues (Title, Network, Classification_of_Request, Full_Name, Last_Name, First_Name, 
Opened_Date, Closed_Date, Status, Priority, Description, Comments, user_name)
  VALUES('#Form.title#', '#Form.network#', '#Form.classification_of_request#', '#Form.full_name#',
         '#Form.Last_Name#', '#Form.First_Name#',<cfqueryparam value="#DateFormat(Form.Opened_Date, 'mm/dd/yyyy')#" cfsqltype="cf_sql_date">, <cfqueryparam value="#DateFormat(Form.Closed_Date, 'mm/dd/yyyy')#" cfsqltype="cf_sql_date">, '#Form.status#', 
         '#Form.priority#', '#Form.description#', '#Form.comments#', <cfqueryparam value="#addun.user_name#" cfsqltype="cf_sql_varchar">)
</CFQUERY>


<script>
              alert("New Service Request has been added successfully!");
              self.location="http://127.0.0.1:8500/TicketTracking/trackertabs.cfm?<cfoutput>user_name=#url.user_name#&tab=2</cfoutput>";
</script>

</cfif>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You first need to know whether you are entering the CFIF or not, I believe you are, but you can test it by putting some code on the page temporarily.  The problem may be because you left out the cfoutput of your script..


<cfif trim(form.title) is "">

        <h1>FLAG - NO FORM TITLE</h1>

        <script>
                      alert("You must select a Request Type!");
                    <cfoutput>
                      self.location="http://127.0.0.1:8500/TicketTracking/addnewsr.cfm?user_name=#url.user_name#";
                    </cfoutput>
        </script>

</cfif>



try

<cfif comparenocase(form.status,"Resolved") and len(trim(form.closed_date)) eq "">

btw i think one cfif close tag is missing in the attached code at line 187
@ansudhindra,  

both IS and EQ already do case insensitive comparisons

the len() function always returns a number, so comparing it to "" will never match

<cfif form.status is "Resolved" and len(trim(form.closed_date)) eq 0>


Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

What is the name of this page: insertnewsr.cfm or addnewsr.cfm? One page is submitted to while the other is redirected to...

It also "sounds" weird to "Add a New Record" when Closing a Request, maybe it's just me.

I would consider removing the "Are you sure prompt" and replacing it with a javascript validation function, where your CFIFs could be moved, AND properly nested (CFIF CFELSE).

Otherwise, replace your <cfif> block with mine:



<cfif Form.status IS "Resolved" AND NOT Len(Form.closed_date)>
	<script>
              alert("Please choose a closed date before closing this service request.");
              self.location="http://127.0.0.1:8500/TicketTracking/addnewsr.cfm?<cfoutput>user_name=#url.user_name#</cfoutput>";
	</script>

	<!--- THIS IS WHERE I'M HAVING THE PROBLEM --->

<cfelseif NOT Len(Form.title)>
	<script>
              alert("You must select a Request Type!");
              self.location="http://127.0.0.1:8500/TicketTracking/addnewsr.cfm?user_name=#url.user_name#";
	</script>

<cfelse>

	<CFQUERY NAME="insert" DATASOURCE="sspsrs">
	INSERT INTO Issues (Title, Network, Classification_of_Request, Full_Name, Last_Name, First_Name, 
	Opened_Date, Closed_Date, Status, Priority, Description, Comments, user_name)
	  VALUES('#Form.title#', '#Form.network#', '#Form.classification_of_request#', '#Form.full_name#',
	         '#Form.Last_Name#', '#Form.First_Name#',<cfqueryparam value="#DateFormat(Form.Opened_Date, 'mm/dd/yyyy')#" cfsqltype="cf_sql_date">, <cfqueryparam value="#DateFormat(Form.Closed_Date, 'mm/dd/yyyy')#" cfsqltype="cf_sql_date">, '#Form.status#', 
	         '#Form.priority#', '#Form.description#', '#Form.comments#', <cfqueryparam value="#addun.user_name#" cfsqltype="cf_sql_varchar">)
	</CFQUERY>


	<script>
              alert("New Service Request has been added successfully!");
              self.location="http://127.0.0.1:8500/TicketTracking/trackertabs.cfm?<cfoutput>user_name=#url.user_name#&tab=2</cfoutput>";
	</script>

</cfif>

Open in new window

I would propose submitting the page to itself in order to eliminate the need for javascript and have the page displayed with error message right in the page.  This method would also keep you from having to redirect and would allow the user to see their form with all their entries still in-tact, highlighting the fields with errors.  

Since you posted your code, I would be happy to mock it up to show you this method, if you're interested.  But if you're not open to using a different approach, I wouldn't want to take the time.  Just let me know.

Personally, I'd choose to validate on the front-end client rather than hit the server, and make the user wait for the request, only to resubmit.

CFinputs and validation also write javascript validation. Actually, I always validate on the client and server.



If you choose to do javascript (pre form submit), it does not take the place of server side validation, it's only an additional validation to keep the user from having to submit when the form isn't complete.

However, the way the code is now, is an odd mix, the user has to submit in order to see javscript..  that's sort-of the lesser of both options.

My personal preference, and usability research has shown, users don't like pop-ups.  Javascript alerts are not well accepted by the user community.  Non-submit validation in the form of ajax on-sreen responses are the newest acceptable format, but those don't use pop-ups.

Author

Commented:
gdemaria - the code below works but I would really see how in your words  "submit the page to itself in order to eliminate the need for javascript and have the page displayed with error message right in the page.  This method would also keep you from having to redirect and would allow the user to see their form with all their entries still in-tact, highlighting the fields with errors".

<cfif trim(form.title) is "">

        <h1>FLAG - NO FORM TITLE</h1>

        <script>
                      alert("You must select a Request Type!");
                    <cfoutput>
                      self.location="http://127.0.0.1:8500/TicketTracking/addnewsr.cfm?user_name=#url.user_name#";
                    </cfoutput>
        </script>

</cfif>

Author

Commented:
thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial