Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

looping through form elements 3

Status
Not open for further replies.

PTmon

IS-IT--Management
Mar 8, 2001
284
US
I have a contact form that I'm inserting into a access database. If the form result is blank, I want to enter NONE into the database. What's the most efficient way to do this? I started to make an if statement and set a variable to none, but there has to be a easier/faster way. Would looping thought the form elements somewhow work?
Thanks,
pt

 
use 'None' instead of NULL
thread232-913594

Human beings, who are almost unique in having the ability to learn from the experience of others, are also remarkable for their apparent disinclination to do so.
-Douglas Adams (1952-2001)
 
Yeah,

<cfif IsDefined("Form.Whatever") AND LEN(Trim(Form.Whatever))>
#Form.Whatever#
<cfelse>
'NONE'
</cfif>

Human beings, who are almost unique in having the ability to learn from the experience of others, are also remarkable for their apparent disinclination to do so.
-Douglas Adams (1952-2001)
 
you can loop through all of the form elements like this.

<cfloop collection = "#form#" item = "elm">
#form[elm]#
</cfloop>

Human beings, who are almost unique in having the ability to learn from the experience of others, are also remarkable for their apparent disinclination to do so.
-Douglas Adams (1952-2001)
 
Can you run UDFs? (CF5 or MX?) Try this..

Code:
<cfscript>
  function FilterNull(fnVar, fnString, dotrim) {
    if (dotrim is "yes") 
      fnvar = trim(fnvar);

    if (fnvar is "")
      return fnString;
    else
      return fnVar;
  }
</cfscript>

#FilterNull(Variable Name,String,Yes/No)#

The first parameter is your variable name... the one you want to safegaurd against nulls.

The second parameter is the string to put in its place.. for instance here you would put "none".

The third parameter instructs the function whether to trim the variable or not... to remove spaces on other side.. if you do trim and the value is just like... " " then the second parameter would appear. If not then " " would be returned because it would be a valid string.

#FilterNull(form.varToVerify,"None",Yes")#

Good luck.

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
you really are liking those udf's aren't you? hehe

That was a good idea, i'd use it over looping more control over your data.

star for thinking outside the box.

Human beings, who are almost unique in having the ability to learn from the experience of others, are also remarkable for their apparent disinclination to do so.
-Douglas Adams (1952-2001)
 
I love UDFs! And I hate Boxes... My next UDF I think will be CrushAndDestroyBox(BoxName,WeaponOfChoice).

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
Yeah, if you're familiar with javascript, ignore the way js works... with being able to call functions created after the call to them..

You can't call a function before its called, so I'd suggest you put the exact code as I put it in application.cfm and you can call it from any of your pages and from custom tags by #Caller.FunctionName(Parameters)#.

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
Ack! This is what I got when I added the script part to my application.cfm file:

Just in time compilation error

Invalid parser construct found on line 1 at position 23. ColdFusion was looking at the following text:
FilterNullInvalid expression format. The usual cause is an error in the expression structure.

The last successfully parsed CFML construct was a CFSCRIPT tag occupying document position (1:1) to (1:10).

The specific sequence of files included or processed is:
D:\inetpub\     


Date/Time: 09/16/04 16:00:53
Browser: Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/125.5 (KHTML, like Gecko) Safari/125.9
Remote Address: 24.140.152.42
HTTP Referrer:

Please inform the site administrator that this error has occurred (be sure to include the contents of this

 
ouch.. I tested the code, it works in cf mx...

Maybe someone here can help you write this as a custom tag.. I've got to jam on some work before I can hit my vacation...

Oh... nevermind... let me try this real quick... if I get a syntax error, someone here can help.

Call the tag like this..
<cf_filternull fnvar="" fnstring="" dotrim="">

Here's the code for filternull.cfm... place it in the same dir as the calling page...
Code:
<cfoutput><cfset fnvar=attributes.fnvar>
<cfif attributes.dotrim is "yes">
  <cfset fnvar = trim(fnvar)>
</cfif>

<cfif not len(fnvar)>
  #attributes.fnstring#
<cfelse>
  #fnvar#
</cfif></cfoutput>

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
I just tested it in CF 5 (the udf). It worked like a charm.

what does your applicaiton.cfm file look like, please post the code. if you are using the function, please post how you are using it also.

lastly what version do you see if you do the following...
<cfoutput><cfdump var = "#server#"></cfoutput>
be sure you're running 5 or better.

Human beings, who are almost unique in having the ability to learn from the experience of others, are also remarkable for their apparent disinclination to do so.
-Douglas Adams (1952-2001)
 
My server version it is 5. I remade the application.cfm file and now I don't get the error. I'm not sure how I implement the tag though? I put

#FilterNull(form.varToVerify,"None",Yes")#

I see the code on the form results page.

As far as cfdump var = #server# that isn't supported in CF5. It would cause an error.

I appreciate all the time you guys are putting in helping me work through this.



 
bleh! Forgot my application.cfm code, sorry:

<cfapplication name="authentication" sessionmanagement="Yes" setclientcookies="Yes" sessiontimeout="#CreateTimeSpan(0,0,60,0)#" applicationtimeout="#CreateTimeSpan(0,2,0,0)#" setdomaincookies="No">
<CFPARAM name="session.LoggedIn" DEFAULT="FALSE">
<cfscript>
function FilterNull(fnVar, fnString, dotrim) {
if (dotrim is "yes")
fnvar = trim(fnvar);
if (fnvar is "")
return fnString;
else
return fnVar;
}
</cfscript>

 
to use the udf you would do something like this

INSERT INTO TABLE (fieldName) values ('#FilterNull(form.fieldName, "none", "yes")#')

however I've found a problem.

I was trying to make something similar only using NULL and not a string. when you use a string you must surround it with '
NULL is not surrounded with '
as this one is written you must code the ' around the function. if you want null it will not work because you'll end up with 'NULL' which is not the same as NULL

I've tried changing the function a little to use strictly NULL and not accept a user defined variable.
in my function
Code:
if (fld neq ""){
  return "'" & fld & "'"
}else{
  return "NULL";
}
here is the problem...
in the query i have

INSERT INTO TABLE (fieldName) values (#FilterNull(form.fieldName)#)
this is fine if the value is ""
however if the value is not "" the query tries to change the returned ' to '' making the total query look like

INSERT INTO TABLE (fieldName) values (''fieldValue'')

this is expected because macromedia tried to idiot proof the ' escaping.

my next attempt was to use PreserveSingleQuotes() i'll call it psq() for short.

i was soon saddened to find that psq does not accept a udf inside the psq call.

INSERT INTO TABLE (fieldName) values (#PSQ(FilterNull(form.fieldName))#)
throws an error. on the first ( for the filterNull function.
I've tested in both cf5 and cf6.1

WITHOUT building a sql string (because that would defeat the entire purpouse) how else can this be done easily?


Human beings, who are almost unique in having the ability to learn from the experience of others, are also remarkable for their apparent disinclination to do so.
-Douglas Adams (1952-2001)
 
Hmm...

Code:
<cfscript>
  function FilterNull(fnVar, fnString, dotrim, sq, psq) {
    if (dotrim is "yes") 
      fnvar = trim(fnvar);

    if (psq is "yes") 
      fnvar = preservesinglequotes(fnvar);

    if (fnvar is "")
      return fnString;
    else
      if (sq is "yes") 
        fnvar = "'" & fnvar & "'";
      if (psq is "yes") 
        fnvar = preservesinglequotes(fnvar);
      return fnVar;
  }
</cfscript>

Try this on the db insert...

#FilterNull(Value,"NULL","Yes","Yes","Yes")#

SQ = Single Quote (whether or not to surround the variable with single quotes.
PSQ = Whether or not to perform PreserveSingleQuotes() on the returned value if the value is not ""?

Gotta get to work now.. I'll check back later.

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
the return is the same so that will not work either.

<cfquery> will try to escape the ' in the return.

Human beings, who are almost unique in having the ability to learn from the experience of others, are also remarkable for their apparent disinclination to do so.
-Douglas Adams (1952-2001)
 
the only alternative is cfsets..

use the original function.. and just do like...

Code:
<cfset fntry=filternull(...)>

And then when inserting fntry...use
#preservesinglequotes(fntry)#

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top