Solved

Help with form feild that is not defined

Posted on 2011-09-14
8
383 Views
Last Modified: 2013-12-24
I need help... I know this is simple but how do you use conditional logic on insert code with form feild that has not been filled out? In other words if a form feild is null I would like for the insert code to be omited. See code
<cfif NOT IsDefined("FORM.Notes")> 
		<!--- Insert Contact Notes--->
        <cfquery name ="addContactNotes" dataSource="#application.datasource#"> 
           INSERT INTO notes (
           ContactID,
           TimeStamp,
           Notes
           )
           VALUES (
           '#newContactPk.GENERATED_KEY#',
           #CreateODBCdate(FORM.TimeStamp)#,
           '#FORM.Notes#'
           )
        </cfquery>
    </cfif>

Open in new window

0
Comment
Question by:jasch2244
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 36539214
Most form fields (not all) will exist, but be empty.  So instead of isDefined, check the value length. If it's 0, the field was left blank.

<cfif len(trim(FORM.Notes)) gt 0>
       FORM.notes has a value. do your insert here ...
<cfelse>
       the field was left blank
</cfif>
0
 
LVL 16

Expert Comment

by:Gurpreet Singh Randhawa
ID: 36539366
Somethig like this as @agx suggested

<!--- Insert Contact Notes--->        
<cfquery name ="addContactNotes" dataSource="#application.datasource#">            
INSERT INTO notes (ContactID, TimeStamp<cfif Len(FORM.Notes) NEQ 0>, Notes</cfif>)
VALUES ('#newContactPk.GENERATED_KEY#',
#CreateODBCdate(FORM.TimeStamp)#<cfif Len(FORM.Notes) NEQ 0>,'#FORM.Notes#'</cfif>)        
</cfquery>
0
 
LVL 1

Author Comment

by:jasch2244
ID: 36539381
What do you think of this solution... it works but after agx's suggestion I'm questioning whether it is the right syntax or something.
<!--- Insert Contact Notes--->
        <cfquery name ="addContactNotes" dataSource="#application.datasource#"> 
           INSERT INTO notes (
           ContactID,
           TimeStamp,
           Notes
           )
           VALUES (
           '#newContactPk.GENERATED_KEY#',
           #CreateODBCdate(FORM.TimeStamp)#,
           '#FORM.Notes#'
           )
        </cfquery>

Open in new window

0
Database Solutions Engineer FAQs

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller single-server environments.

 
LVL 1

Author Comment

by:jasch2244
ID: 36539386
Sorry this is the right one I copied and pasted too soon.
<cfif NOT IsDefined("FORM.Notes") OR #FORM.Notes# EQ ""> 
    
    <cfelse>
		<!--- Insert Contact Notes--->
        <cfquery name ="addContactNotes" dataSource="#application.datasource#"> 
           INSERT INTO notes (
           ContactID,
           TimeStamp,
           Notes
           )
           VALUES (
           '#newContactPk.GENERATED_KEY#',
           #CreateODBCdate(FORM.TimeStamp)#,
           '#FORM.Notes#'
           )
        </cfquery>
    </cfif>

Open in new window

0
 
LVL 52

Expert Comment

by:_agx_
ID: 36539507
No, I'd go with the other code.  

>> <cfif NOT IsDefined("FORM.Notes") OR #FORM.Notes# EQ ""> 

That doesn't make sense to me. The 1st part is saying if the field DOESN'T exist - do the insert.  Assuming FORM.Notes is a text field or text area, it'll always exist. So the isdefined part does nothing.  The 2nd part is okay (once you remove the extra # signs). But it still lets users enter all white space.  Usually you'd want to ignore that.

So I think the length check I posted is  better.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 36539546
I just noticed the <cfelse> part.  Generally you don't want to structure code like that because it's very confusing.  Always write cfif's in positive terms because it's more readable. Also skip any empty clauses that don't do anything.  You'd only do that when debugging.

Notice how it's much more obvious what this statement does

<cfif someCondition is true>
      run an insert
</cfif>

... than if you wrote it this way

<cfif NOT someCondition>
      do nothing
<cfelse>
      run an insert
</cfif>

0
 
LVL 1

Author Closing Comment

by:jasch2244
ID: 36539570
As always thank you
0
 
LVL 52

Expert Comment

by:_agx_
ID: 36539599
Welcome :)
0

Featured Post

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question