user1641524
user1641524

Reputation: 15

PHP get value after '&' not taking?

In the following code....

<td height="27" align="right" valign="middle"><a href="General.php?category=Social & Political" target="_parent">Social & Political</a></td>

And in General.php page....

<input type='text' name='category' id='category' value='<?= $_GET['category']; ?>' maxlength="25"/>

Here the value taking only 'Social', instead of 'Social & Political'. If I change this to 'Social Political', its taking perfectly. Why is that?

Upvotes: 0

Views: 140

Answers (4)

Hernan Velasquez
Hernan Velasquez

Reputation: 2820

To complement the previous answers, this will work

a href="General.php?category=Social %26 Political"

%26 is the url code for the character &

Upvotes: 0

techie_28
techie_28

Reputation: 2133

use urlencode for the variables which you are passing in the query string

Upvotes: 0

Quentin
Quentin

Reputation: 943569

& has special meaning in both URIs and in HTML. In a query string, which is where you are putting it, it means "End of this key/value pair and start of the next one".

Run your string through urlencode (immediately) before inserting it into a URL.

Run your string through htmlspecialchars (immediately) before inserting it into HTML.

(urlencode shouldn't leave any HTML special characters in the string, but it is a good habit to push all data that is "not known to be HTML" through htmlspecialchars before concatenating it with HTML).

Upvotes: 4

Cosmin
Cosmin

Reputation: 1490

Because & is a special character.

Use PHP function urlencode() and urldecode()

Upvotes: 1

Related Questions