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

 
I think i've come to that conclusion too.

<cfset sql = filternull(form.fld1) & "," & filternull(form.fld2) & "," & filternull(form.fld3) & "," & filternull(form.fld3) & "," & filternull(form.fld4)>

<cfquery datasource = "#application.dsn#">
INSERT INTO statusInfo (fld1, fld2, fld3, fld4, fld4)
VALUES (#preserveSingleQuotes(sql)#)
</cfquery>

I asked macromedia if they plan to fix it. we'll see what they say, if they reply.

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)
 
its still less text than
<cfif isdefined("form.fld1") and trim(len(form.fld1))>
'#form.fld1#'
<cfelse>
NULL
</cfif>
and so on.

I hope they fix it. it would be nice to avoid all that mess.

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)
 
Pretty sad, gotta admit, its a beautiful function too.

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.
 
it's pretty nice. I used to do the same thing in ASP. but then forgot about it into CF because the cfscript language uses different syntax than cf. asp syntax is the same all over.

i was going to add an option to specify what kind of field it was to determin how to wrap the return. ' or # or nothing for NULL and numbers. I haven't decided if i'm going to use it yet. I may, but don't like the idea of having to use cfset to do it.

-MM’s conveniences are hindering me. :|

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)
 
This was one of my first ASP functions... ain't it p'erty?

Code:
function textcheck(texttocheck)
  if isnull(texttocheck) or len(trim(texttocheck)) = 0 then
    textcheck = "NULL"
  else
    textcheck = "'" & REPLACE(texttocheck, "'", "''") & "'"
  end if
end function

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)
 
still fighting with this :)
Here's my query for now, lots of fields to be added later, once this works!:

<cfquery name="addcontact" datasource="mydata" username="myuser" password="mypass">
INSERT INTO contacts (fname, lname, email, company, category, comments, address, city, state, country, zip, phone)
values('#FilterNull(form.fieldName, "NONE", "yes")#')
</cfquery>

Getting error:
Error Occurred While Processing Request
Error Diagnostic Information
Just in time compilation error
Invalid parser construct found on line 4 at position 22. 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 (3:9) to (3:18).

The specific sequence of files included or processed is:
D:\inetpub\
 
did you change the application file again? above you said you changed it and now you don't get an error. did you change it again? it's erroring out on the application page. lets have a look if you did.

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)
 
Try this one.

<CFFUNCTION NAME="FilterBlank">
<CFARGUMENT NAME="fnVar">
<CFARGUMENT NAME="fnString">
<CFARGUMENT NAME="dotrim">
<CFIF dortrim EQ Yes>
<CFOUTPUT>
#IIF(fnVAr EQ "", DE("fnString"),DE("fnVAr"))#
</CFOUTPUT>
</CFIF>
</CFFUNCTION>

Joel
 
If that works, great, but it should run into the same problem mine did... mine works great, except with PreserveSingleQuotes...

Stupid PreserveSingleQuotes().

If it works no better or no worse than mine, I'd suggest mine...

IIf is a slow function and so is DE.. so combined, they're a bit slow.. But if it is what works, it is what works..

Stupid PreserveSingleQuotes().

And JoelXez.. just so you know.. the issue here is not syntax but how to return a quoted fnVar but a non-quoted fnString (such as NULL).. When passed in a variable, single quotations are doubled so they don't trip the database.. IE..

My dog's name is Fido.

would be

My dog''s name is Fido.

<cfset strme="My dog's name is Fido">
#PreserveSingleQuotes(strme)#

would make sure that it comes out with a single quote.. The problem here is that PreserveSingleQuotes, as some other functions do, refuses to surround a function.. for instance...

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.
 
actually, ptmon is having a problem with the script.
No problem you probably just didn't read the error all the way through.
according to his error message he's getting an error on the bolded line.
what happens if you take it out of the application.cfm file and put it in an included file?

<cfscript>
function FilterNull(fnVar, fnString, dotrim) {
if (dotrim is "yes")
fnvar = trim(fnvar);
if (fnvar is "")
return fnString;
else
return fnVar;
}
</cfscript>

I agree...
Stupid PreserveSingleQuotes().

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)
 
Oh I know that bombboy, I was talking about the ongoing problem here..

And I'd say since it runs for you and it runs for me that he tried one of our corrections, probably the second version I tried to write.

Reverting to the working version should work fine.

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