Skip to main content
Participating Frequently
July 29, 2011
Answered

Error when number sign # in address field of form

  • July 29, 2011
  • 1 reply
  • 2277 views

I need to be able to accept the number sign # in my form field for address, ie apt #3. When a # is submitted with my form, I get an error. How do I allow the # to be included for this field?

<cfinput style="WIDTH: 247px" size="30"  type="text" name="shipaddress" value="" required="yes" message="You must enter your address">

This topic has been closed for replies.
Correct answer Adam Cameron.

Yes. I was hoping to fix the # in the shipaddress, then look into these other areas of concern in the future. I don't want to start too many things at once. Could you suggest how I should use the escape function in my code?


Well...you don't say what you found (it would really help if you didn't assume we had telepathy...), but let's pretend it was urlEncodedFormat() you discovered.  Because that'd be the right function to use ;-)

So... having discovered that, have you read the docs for it?  http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7bdf.html

Have you just googled it to see any other examples in use?

Do you understand how CF functions work / are used (this might seem like a stupid question, but the wording of your post earlier makes me unsure... but ti could just be the wording... or my reading thereof).

Basically if you have a value in a URL which contains characters that are meaningful to the URL (like &, =, %, #, etc), you need to use urlEncodedFormat to convert the characters into a URL-safe encoded representation of them.

So if you had this value "here's a # sign" that you wanted to put in a URL, you'd need to use urlEncodedFormat() to escape it:

http://yourdomain.com/path/to/file.cm?param=#urlEncodedFormat("here's a # sign")#

Basically anything user-entered needs to be escaped, because you cannot predict what a user will enter...

--
Adam

1 reply

Owainnorth
Inspiring
July 29, 2011

When a # is submitted with my form, I get an error.

Okay, and what did the error say? When you get an issue like this, try and break it down to the smallest possible case people can test. As it's just a quickie, I knocked up this page:

<!--- test.cfm --->

<cfdump var="#form#">

<cfform>

  <cfinput style="WIDTH: 247px" size="30"  type="text" name="shipaddress" value="" required="yes" message="You must enter your address">

</cfform>

And when I put in "#3" and submit, it appears in my form scope just fine. I'm using CF9, so unless there are operational differences between my version and yours (which I doubt on something so small) the problem lies elsewhere. The hash symbol can cause issues (as it's a "reserved character" almost) so depending on how you're written your processing code it could cause problems.

Go through your code stripping it out line by line until you find the problem line.

O.

KatesF21Author
Participating Frequently
July 29, 2011

Thank you for your help. I am very new here.

The form collecting the address is on my shopping cart and is working fine, I ran the test code you provided.

The data from the form is then inserted into a database.

intodb.cfm

INSERT INTO people(firstname,lastname,email,address,city,state,country,zip,phone,shipfirst,shiplast,shipaddress,shipcity,shipstate,shipzip,shipcountry,lastfour,frequency,cases,ccmo,ccyr,cctype,dater,cfid,cftoken)
VALUES('#shipfirst#','#shiplast#','#email_addr#','#shipaddress#','#shipcity#','#shipstate#','#shipcountry#','#shipzip#','#phone#','#shipfirst#','#shiplast#','#shipaddress#','#shipcity#','#shipstate#','#shipzip#','#shipcountry#','#lastfour#','#frequency#','#cases#','#ccmo#','#ccyr#','#cctype#','#DateFormat("#Now()#", "yyyy-mm-dd")# #TimeFormat("#Now()#", "hh:mm:sstt")#','#session.cfid#','#session.cftoken#')

<cflocation url="sale2.cfm?shipmethod=#shipmethod#&shipcost=#shipcost#&shipfirst=#shipfirst#&shiplast=#shiplast#&shipaddress=#shipaddress#&shipcity=#shipcity#&shipstate=#shipstate#&shipzip=#shipzip#&shipcountry=#shipcountry#&country=#shipcountry#&phone=#phone#&email_addr=#email_addr#&ccnum=#ccnum#&ccmo=#ccmo#&ccyr=#ccyr#&cctype=#cctype#&ordertotal=#ordertotal#&disc=#disc#">

Then on sale2.cfm.

<cfsavecontent variable="strXML">
<?xml version="1.0" standalone="yes"?>
<SaleRequest>
<CustomerData>
<Email>#email_addr#</Email>
<BillingAddress>
<Address1>#shipaddress#</Address1>
<FirstName>#shipfirst#</FirstName>
<LastName>#shiplast#</LastName>
<City>#shipcity#</City>
<State>#shipstate#</State>
<Zip>#shipzip#</Zip>
<Country>#shipcountry#</Country>
<Phone>#phone#</Phone>
</BillingAddress>

I receive this error, assuming it is the EMAIL_ADDR variable because it is the first one:

Variable  EMAIL_ADDR is undefined.

Owainnorth
Inspiring
July 29, 2011

I have been looking into how to "escape" a reserved character for the shipaddress. I now understand how the function works, but not sure how to implement it. Do I add this to my url below or do I need to add the function to the code before it runs the url?

<cflocation url="sale2.cfm?shipmethod=#shipmethod#&shipcost=#shipcost#&shipfirst= #shipfirst#&shiplast=#shiplast#&shipaddress=#shipaddress#&shipcity=#shipcity#&shipstate=#shipstate#&shipzip=#shipzip#&shipcountry=#shipcount ry#&country=#shipcountry#&phone=#phone#&email_addr=#email_addr#&ccnum= #ccnum#&ccmo=#ccmo#&ccyr=#ccyr#&cctype=#cctype#&ordertotal=#ordertotal #&disc=#disc#">


Right, well first up why do you need to pass all those through the URL bar? What happens if someone changes it? Why not just pass the id you just inserted, then re-query the other side?

Some browsers have an URL-length limit, so you want to be careful about that.