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?


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


Look it up.

Member Avatar for diafol

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


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.

commented: good spot +4
commented: Thanks +1
Member Avatar for diafol

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.