IAmNoob
IAmNoob

Reputation: 641

Dynamic URL in PHP not working when passing "&" in href tag

I have the following URL

http://localhost:8777/business.php?id=Mobiles and Tablets 

When I am passing "and" in id it is redirecting me to the desired page but when I am passing

http://localhost:8777/business.php?id=Mobiles & Tablets 

& i.e. ampersand in id it is giving 404 error.

My PHP Code is like this:

<a href="business.php?id=<? echo $cat;?>"><? echo $cat;?></a>

Upvotes: 3

Views: 1646

Answers (2)

fortune
fortune

Reputation: 3372

use php urlencode()

This function is convenient when encoding a string to be used in a query part of a URL, as a convenient way to pass variables to the next page.

And use urldecode() to decode your encoded argument

Decodes any %## encoding in the given string. Plus symbols ('+') are decoded to a space character.

Upvotes: 2

Ravi Dhoriya ツ
Ravi Dhoriya ツ

Reputation: 4414

You have to use urlencode() in your php code.

<a href="business.php?id=<? echo urlencode($cat) ;?>"><? echo $cat;?></a>

It will generate link like:

http://localhost:8777/business.php?id=Mobiles+%26+Tablets

Space will be converted to +, and & will be converted to %26

Upvotes: 3

Related Questions