Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php variable printing extra numbers

I working on a small form using PHP, the user enter a code a code and if the code is not found the form need to go back, I am trying to do that using this code but it not working

the $code contains the id of the page which is 208, but for some reason is not only printing 208 it is printing %20208 any ideas why is doing this.

<?php
die('<center>code dont exist<br>Try again!</center>   
             <script type="text/javascript">
               function backToMain() { 
               window.location = "codes.php?id= '.$code. '"; 
            }
               setTimeout("backToMain()", 3000);
             </script>');
like image 761
Carlos Perez Avatar asked Feb 05 '26 11:02

Carlos Perez


2 Answers

You put a space between id= and the beginning of the $code. The %20 is a space, once the URL is correctly encoded. Remove it:

<?php
die('<center>code dont exist<br>Try again!</center>   
         <script type="text/javascript">
           function backToMain() { 
           window.location = "codes.php?id='.$code.'"; 
        }
           setTimeout("backToMain()", 3000);
         </script>');

If your $code can contains space, you should also apply the trim() function on it.

like image 159
Maxime Lorant Avatar answered Feb 08 '26 00:02

Maxime Lorant


trim will remove all spaces. also remove space after id=.

try this:

<?php
die('<center>code dont exist<br>Try again!</center>   
             <script type="text/javascript">
               function backToMain() { 
               window.location = "codes.php?id='.trim($code). '"; 
            }
               setTimeout("backToMain()", 3000);
             </script>');
like image 29
Awlad Liton Avatar answered Feb 08 '26 00:02

Awlad Liton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!