0

i use javascript to send usernames with javascript and $_POST method. its work with simple usernames but with username that have special characters such as " Al[!]e$E" have problem and cannot find username in my database.

<div class="friend-add">

<form method="post" action="" class="form-inline">
<fieldset>
<input name="username" id="username" placeholder="Username" autocomplete="off" type="text">
<button id="add_friend" name="add_friend" type="submit" class="btn">Add friend</button>
</fieldset>
</form>
<div id="loading_m" style="position:relative; width:16px; height:16px; float:left; margin-top:-30px;margin-left:330px;background-image:url(loading.gif); background-repeat:no-repeat; background-size:16px 16px; display:none;"></div>
</div>


<script language="javascript" type="text/javascript">
$("#add_friend").click(function(){
$("#add_friend").attr("disabled","disabled");
$("#loading_m").fadeOut('fast');
$("#loading_m").fadeIn('fast');
$("#alert_m").html("");
$.post("friend_request.php", { friend_username : $("#username").val() })
    .done(function(data) {
        //alert(data);
        if (data.toLowerCase().indexOf("notexist") >= 0){
            $("#loading_m").fadeOut('fast',function(){
                $("#alert_m").html("<b style='color:#FF0040;'>This user does not exist.</b>").fadeIn('fast').delay(5000).fadeOut('fast');
                $("#add_friend").removeAttr('disabled');
            });
        }

        ......

                .fail(function(){
        $("#loading_m").fadeOut('fast',function(){
                $("#alert_m").html("<b style='color:#F00;'>Something wrong with server please try again later.</b>").fadeIn('fast').delay(5000).fadeOut('fast');
                $("#add_friend").removeAttr('disabled');
            });
     });
     return false;


});
TEK
  • 1,265
  • 1
  • 15
  • 30
user2997361
  • 111
  • 1
  • 2
  • 6

1 Answers1

0

Why you should use encodeURIComponent: When are you supposed to use escape instead of encodeURI / encodeURIComponent?

...
$.post("friend_request.php", { friend_username : encodeURIComponent($("#username").val()) })
...

Edit: Use urldecode() in php to turn it back into the original string.

$username = urldecode($_POST['friend_username']);

Ref: http://www.php.net/manual/en/function.urldecode.php

Community
  • 1
  • 1
Synthetx
  • 599
  • 4
  • 13