0

So I'm running a script from my page using the Jquery post wrapper, and in chrome debugger the below cases happening:-

1) if the username is taken i'm getting `"taken"` 

2) else it echo's nothing. 

However, I cannot get to the alert('im here'); line it seems that if(data=="taken") statement is never run or always evaluating to false, which is not the case. What am I doing wrong?

Jquery

 var flag;
    $.post('welcome/check_username',{childname: $('#name').val()}, function(data){
    if(data=="taken")
            {
                alert('im here');
                 return flag = true;
                  //tell user that the username already exists
                  }else{
                   return flag = false;
                      //username doesn't exist, do what you need to do
                       }
                       });

PHP

if ($this->Form_builder->check_unique_username($username, $childname))
{
     echo "taken";
}

4
  • 1
    add console.log(data); and check the value of data. Commented Aug 14, 2013 at 8:06
  • from where You want to check unique username? Jquery / PHP ? Commented Aug 14, 2013 at 8:09
  • console.log(data) is empty when username is unique otherwise it has "taken" Commented Aug 14, 2013 at 8:12
  • were do you define the php file to make the ajax call to? Commented Aug 14, 2013 at 8:15

3 Answers 3

1

its always good to trim things. try

  if(data.trim()=="taken")
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

var flag;
var result = $.post('welcome/check_username',{childname: $('#name').val()});
result.done(function( data )
{
    if(data=="taken")
    {
        alert('im here');
        return flag = true;
        //tell user that the username already exists
    }else{
        return flag = false;
        //username doesn't exist, do what you need to do
    }
});

Comments

1

Looking at the code, here are the sections I would check:

  1. On the PHP side, check whether the function is properly getting the proper values in $username and $password. You can do this by modifying the PHP file like this:

    if ($this->Form_builder->check_unique_username($username, $childname))
    {
     echo "taken";
    }
    else
    {
      echo $username." and ".$childname." is not taken";
    }
    
    1. Then on the jQuery side, before the if statement, do console.log(data) or alert(data) to see what is being returned. Perhaps, you are sending data by the POST method but the server side script expects the parameters via the GET method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.