lIP TT lIP TT - 4 months ago 8
Ajax Question

Sending # symbol with AJAX to PHP

I have got this Ajax that send comment's text to PHP

$.ajax({
type: "GET",
url: '../files/ajax.php',
data: "C=" + cc+"&I="+i,
success:function(data) {
alert(data);
}
});

if (isset($_GET["I"]) && isset($_GET["C"])) {
$RandS=$_GET["I"];
$Comment=$_GET["C"];
$Comment=trim($_GET["C"]);
$Comment=htmlspecialchars($_GET["C"]);
echo $Comment;
}


When comment is something like
Hope you like pancakes
It returns everything perfectly,But when comment is
'#I #Like pancakes'
it does not return anything except error

Uncaught SyntaxError: Unexpected end of JSON input

Answer

You need to URL-encode the parameters if they contain special characters. When using $.ajax, the best way to ensure that they're encoded properly is to use an object rather than a string for the data: option.

$.ajax({
    type: "GET",
    url: '../files/ajax.php',
    data: { C: cc, I: i },
    success:function(data) {
        alert(data);
    }
});
Comments