954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

# symbol in get statement

Hi All,

I am processing a form that includes address info. If the user fills out the form and (address2) has an apartment number that they enter like:

#1522

I have trouble later where I might contruct a link such as:

header("location: contact.php?address1=$address1&address2=$address2&city=$city&state=$state");


The link creates fine, but processing on the otherside where I am trying to get the data back stops after encountering the # sysmbol. The code I have to get the data from the link is:

if(isset($_GET["address2"])) $address2 = $_GET["address2"]; else $address2 = "";


The address2 field is empty as are all the fields that come after it in the process. Any ideas what to do to allow the # symbol?

Thanks.

andym67
Newbie Poster
14 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

try using & #35 ; instead (without the spaces),

see: http://www.ascii.cl/htmlcodes.htm

pritaeas
Posting Expert
Moderator
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

I don't understand. The user enters the # sign into the field and my HEADER: then uses it. Is there a way to surround it with quotes etc in my code to somehow encode it or something.

I am not understanding your suggestion. Thanks.!

andym67
Newbie Poster
14 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

My created url looks like this:

http://www.domain.com/contact.php?address1=123%20Main%20 Street&address2=#1522&city=chicago

As you can see, the # is probably my problem.

andym67
Newbie Poster
14 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

urlencode()

Look it up.

kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
 

The text after a # in a querystring is taken to be an anchor or bookmark. You should encode the data before passing it to a querystring.

Use urlencode and htmlentities:

$q_string = 'address1=' . urlencode($address1) . '&address2=' . urlencode($address2) . '&city=' . urlencode($city) . '&state=' . urlencode($state);
header( 'Location: contact.php?' . htmlentities($q_string));


//EDIT
sorry KK, you posted as I was typing

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

urlencode()

Look it up.

I wasn't sure what I needed to look up or I would have done so. As I am very new to PHP I didn't know the urlencode() existed.

But, thank you very much as that was what I needed. It works wonderfully. I appreciate your help.

andym67
Newbie Poster
14 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

Very helpful - Thanks.!!! Didn't know about urlencode()

andym67
Newbie Poster
14 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

As ardav says, you need urlencode, but you should not use htmlentities. YOu only use htmlentities when you are outputting HTML. The header is not HTML - it's part of the underlying HTTP protocol.

edwinhermann
Junior Poster
141 posts since Sep 2009
Reputation Points: 67
Solved Threads: 29
 

Yes, right enough, I cobbled the code from a cgi script. No need for h.e.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: