Solved

Help with form feild that is not defined

Posted on 2011-09-14
8
382 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Best practices for sharing large amounts of data 9 118
http to https 3 83
ajaxSubmit is giving me an error 1 63
cfscript coding help 4 40
Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

739 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