Search code examples
phpunicodeencodeuricomponent

How to give encodeURIComponent for a link


I need to give encodeURIComponent for a link. This question is related to the answer of PHP variable error in unicode. please help me.

<?php
header('Content-Type: text/html; charset=utf-8');
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
$NewValue="";
if(!empty($_GET['NewValue'])){
    echo $NewValue=$_GET['NewValue'];//this variable is the problem;
    }
$Value="நன்றி";
?>
<a href="test1.php?NewValue=<?php echo $Value;?>">Click here</a>                
</body>
</html>

Solution

  • In the other question before you were using JavaScript to append the value to the URL, therefor encodeURIComponent was the right choice.

    The problem now is basically the same – here

    <a href="test1.php?NewValue=<?php echo $Value;?>">Click here</a>
    

    you are also putting a variable into a URL context (only this time using PHP), and so it should be properly encoded as well.

    urlencode is kinda like PHP’s “version” of encodeURIComponent – so:

    <a href="test1.php?NewValue=<?php echo urlencode($Value);?>">Click here</a>