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:


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?


7 Years
Discussion Span
Last Post by diafol

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.!


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));

sorry KK, you posted as I was typing

Edited by diafol: n/a



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.


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


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.

Votes + Comments
good spot
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.