Skip to content
Advertisement

PHP not comparing “#”

I have a simple script where I enter some text on my search bar and PHP echo the first character from the text.

My code:

$data = strval($_GET['u']);
$ss = substr($data, 0,1);

if ($ss == "@") {
    echo("@");
} else if ($ss == "#")  {
    echo("htttt");
} else {
    echo($ss);
}

Here everything is working fine but the # is neither being compared nor being echo. It just shows blank. I tried to convert it into a string using strval() but it’s not working too! What should I do?

my ajax call from jquery:

$("#search").on('input', function(event) {
            event.preventDefault();
            var sv = $("#search").val();
            $.get('phpscripts/short/searchdata.php?u='+sv, function(data) {
                $("#s-h").text(data);
            });

});

Advertisement

Answer

Actually, # is a fragment that is not sent to the server and also the part coming after it. So you will never get # by $_GET['u'], until you decode it.
Since the UPDATE you can control the querystring. So encode query in Client side (JS). Then decode it in Server side (PHP) back.
JS:

encodeURIComponent("#"); // %23

PHP:

urldecode('%23'); // #
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement