简体   繁体   中英

PHP : urldecode($str) not working

i am watching php lynda lecture . Following is a simple example to elaborate urldecode() function but i dont know why this is not working for me . Please guide me whats wrong with it

first_page.php

<?php $link_name='Second page' ;
      $id=2;    
      $name="Johnson & Johnson";
 ?>
<a href="second_page.php?id=<?php echo $id ?>&name=<?php echo urldecode($name);?>"><?php echo $link_name;?></a>

second_page.php

<?php  $id=$_GET['id']; 
           $name=$_GET['name'];
           echo $id.'<br/>'; 
           echo $name;          
?>

output

2 Johnson // for me it should be Johnson & Johnson and this is url string

id=2&name=Johnson%20&%20Johnson

why is it not encoding properly ? Please help

Its not working because you are using the wrong function:

&name=<?php echo urldecode($name);

It should be urlencode()

<a href="second_page.php?id=<?php echo $id ?>&name=<?php echo urlencode($name);?>"><?php echo $link_name;?></a>
                                                                ^^^ encode not decode

Also another way:

<?php
$link_name = 'Second page';
$id = 2;    
$name = "Johnson & Johnson";

$query_string = array('id' => $id, 'name' => $name);
$query_string = http_build_query($query_string);

?>
<a href="second_page.php?<?php echo $query_string; ?>"><?php echo $link_name; ?></a>

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