Musashi
Musashi

Reputation: 25

Trying to validate form with ajax. Same code, different behaviour

I'm working on a project in my dev site that has exactly the same code as my production one.

It's a simple form validation using ajax. Here's the code for dispuser.php:

$username = protect($_POST['user_name']);
$dispuser = new user();
$dispuser->username = $username;
$res = $dispuser->get("all");
$num = mysql_num_rows($res);

if ($num>0)
{
    echo "no";
}
else
{
    echo "yes";
}

Here's the javascript code:

$(document).ready(function()
{
    $("#username").blur(function()
    {
        $("#msgbox").removeClass().addClass('messagebox').text('Verificando...').fadeIn("slow");
        $.post("./dispuser.php",{ user_name:$(this).val() } ,function(data)
        {
            if(data=='no')
            {   
                document.getElementById("submitBT").disabled = true;
                $("#msgbox").fadeTo(200,0.1,function()
                { 
                    $(this).html('Not available').addClass('messageboxerror').fadeTo(900,1);
                });     
            }
            else
            {
                $("#msgbox").fadeTo(200,0.1,function()
                { 
                    document.getElementById("submitBT").disabled = false;   
                    $(this).html('Available!').addClass('messageboxok').fadeTo(900,1);           
                });
            }
        });
    });
});

And here's the html code:

<td><label for="username">Usuário: </label></td>
<td><input type="text" name="username" id="username" /></td>
<td><span id="msgbox" style="display:none; no-padding;"></span></td>

I already tracked the response with firebug and everything is ok! (returns "yes" when user available and "no" when not available.

I have the exact same code in: pp.atoanavida.com.br/registro.php and in www.pesometro.com.br/registro.php

You can try with user "atoanavida". It's not available and both databases are exactly the same.

My problem: My production site is working OK, showing the right message when the user is/isn't available. On my dev site, which has the same code, the script always shows "user available".

Upvotes: 1

Views: 270

Answers (1)

ingo
ingo

Reputation: 5569

For some reason the data returned is "\nNo" as in NewLine and then no. You could fix this php side or simply trim data in javascript before the if(data == 'no')...

like so:

data = data.replace(/^\s+|\s+$/g,"");

Upvotes: 2

Related Questions