Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help with form feild that is not defined

Posted on 2011-09-14
8
Medium Priority
?
386 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 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
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 …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

971 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