简体   繁体   中英

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 

& ie ampersand in id it is giving 404 error.

My PHP Code is like this:

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM