1

I'm doing an ajax request with jquery to check whether a username is taken or not in a database and changing text inside a div (in this case a div with id="wnguser"). I am able to get the response from the php script and the text does appear where it should (I echo one of two strings: "username available" or "username unavailable" from the php script). I am not able though to use the response from the php script, the variable data (in this case one of the two strings mentioned above) to make a decision.

For example the the if() which I marked as //DOES NOT WORK. Can someone tell me how to make the if() work or the correct way to take a decision base on the response of the php script which is one of two strings.

Thank You

<script type="text/javascript">

        $(document).ready(function(){

        $('#username').keyup(function(){

            var user = $('#username').val();
            $.post('checkusername.php', {username: user}, function(data){

            $('#wnguser').html(data);

            //DOES NOT WORK
            if (data === "username available"){
                 execute some code
            }

            });

        });



        });

    </script>
Meenesh Jain
  • 2,532
  • 2
  • 19
  • 29

3 Answers3

0

In your checkusername.php you have to give the condition instead of the current file.

-1
if (String(data).trim() === "username available"){
                 //execute some code
            }
Mahesh
  • 1,063
  • 1
  • 12
  • 29
-2

use this:

if (data == "username available"){
    execute some code
}

Refer this for more info

Community
  • 1
  • 1
amit_183
  • 961
  • 3
  • 19
  • 36