Solved

Help with form feild that is not defined

Posted on 2011-09-14
8
377 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
  • 4
  • 3
8 Comments
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
Comment Utility
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 15

Expert Comment

by:myselfrandhawa
Comment Utility
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
Comment Utility
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
 
LVL 1

Author Comment

by:jasch2244
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 52

Expert Comment

by:_agx_
Comment Utility
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_
Comment Utility
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
Comment Utility
As always thank you
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
Welcome :)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readinā€¦
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ā€¦
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now