Dynamic url in PHP doesn't work when passing "&" in href tag
I have the following url
http://localhost:8777/business.php?id=Mobiles and Tablets
When I pass "and" in id it redirects me to the correct page, but when I pass
http://localhost:8777/business.php?id=Mobiles & Tablets
& that is the ampersand in id, it gives a 404 error.
My PHP code looks like this:
<a href="business.php?id=<? echo $cat;?>"><? echo $cat;?></a>
source to share
You have to use urlencode()
in your php code.
<a href="business.php?id=<? echo urlencode($cat) ;?>"><? echo $cat;?></a>
It will generate a link like:
http://localhost:8777/business.php?id=Mobiles+%26+Tablets
Space
will be converted to +
, and &
will be converted to%26
source to share
use php urlencode ()
This function comes in handy when encoding a string that will be used in the URL request part, as a convenient way to pass variables to the next page.
And use urldecode () to decode your encoded argument
Decodes any% ## encoding on the given string. Plus characters ('+') are decoded with a space character.
source to share