Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help with form feild that is not defined

Posted on 2011-09-14
8
Medium Priority
?
384 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 2000 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Supports up to 4K resolution!

The VS192 2-Port 4K DisplayPort Splitter is perfect for anyone who needs to send one source of DisplayPort high definition video to two or four DisplayPort displays. The VS192 can split and also expand DisplayPort audio/video signal on two or four DisplayPort monitors.

Question has a verified solution.

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

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…
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

730 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