Ampersand using Javascript & PHP
I'm trying to redirect a page using javascript output using php. The problem I'm having is passing the ampersand as you can see below.
Input
开发者_如何学C$url = 'number=1&id='.$usrid->id;
echo "<script type='text/javascript'>
window.location = 'directory?$url';
</script>";
Output
The above return on browser address bar this:
http://www.domain.com/directory/?number=1#038;id=190
Escape the URL first:
$url = htmlspecialchars('number=1&id=' . $usrid->id);
Resolution to the above issue exactly as 'yar' suggests. The redirect was happening too fast and the php script didn't stop early. A simple exit function fixed the issue after redirect code.
Use the html_entity_decode
function
$url = 'number=1&id='.$usrid->id;
echo "<script type='text/javascript'>
window.location = 'directory?".html_entity_decode($url)."';
</script>";
精彩评论