1

I've been trying to build a registeration form for a website I am building. I can do the basics but I want it to check the username availability without reloading the page.

JAVASCRIPT

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script>
    $(document).ready(function()
    {
        $("#Username").focusout(function()
        {
            //Check if usernane if available
            var username = $("#Username").val();
            $.post("scripts/check_username.php", {username:  username}, function(data)
            {
                if(data == 'false')
                {
                    alert('Username not available');
                    $("#Username").setCustomValidity("This username is already taken!");
                }
                else
                {
                    alert('Username available');
                }
            });
            return false;
        });
    });
</script>

HTML

<form id="registerForm">
                <table>
                    <tr><td>Username</td><td><input id="Username" class='textInput' type='text' name='username' required></td></tr>

PHP SCRIPT

<?php
include 'open_connection.php';
$result = 'true';
$username = mysql_real_escape_string($_POST['username']);

$result = mysql_query("SELECT * FROM tblMembers WHERE Username='$username'");
while($row = mysql_fetch_array($result))
{
    $result = 'false';
}
echo $result;
?>

When I leave the textbox it says username available no matter what. I placed a username "test" in the database... no luck

Please help

4
  • 1
    And your question is? Also, mysql_* functions are deprecated; you should be using mysqli_*. Commented Sep 30, 2013 at 1:18
  • So what's your problem? Commented Sep 30, 2013 at 1:20
  • Sorry about that, forgot to post the problem Commented Sep 30, 2013 at 1:21
  • I want to get the value of that textbox. Commented Sep 30, 2013 at 1:25

3 Answers 3

1

PHP:

$output = 'true';
$username = mysql_real_escape_string($_POST['username']);
$result = mysql_query("SELECT * FROM tblMembers WHERE Username='$username'");
while($row = mysql_fetch_array($result))
{
    $output = 'false';
}
echo $output;

Ans Script:

<script>
$(document).ready(function()
{
    $("#Username").focusout(function()
    {
        //Check if usernane if available
        $.post("scripts/check_username.php", {username:  $("#Username").val()}, function(data)
        {

            if(data  =='false')
            {
                    $("#Username").setCustomValidity("This username is already taken!");
                }
                else
                {
                    alert('Username available');
                }
            });
            return false;
        });
    });
</script>

By the way, don't use mysql_* function, they're deprecated. use Mysqli or PDO. Next thing is you forgot to put semi-colon that the end of your statements !

Sign up to request clarification or add additional context in comments.

6 Comments

That's your connection or your server!
It was an error with my side, it doesn't work either. sorry for the confusion
Im still getting username availble when I use "test"
Use two variables in php like I did otherwise you get what you get now!
I'm sorry I don't see where you used two variables... sorry to be a pain
|
0

You specify $("#Username").value do you mean to use $("#Username").val()?

$('#Username').value is probably returning you rubbish which will not exist in your DB.

Comments

0

Do you use Developer Tools or Firebug?

It would be easy to see where the issue lies if you examined the $.post to check_username.php:

(1) is the correct post request being sent? I think your data object should be {"username":username}

(2) is your script responding true? or something else? You only know it's not returning false by the way your if statement is structured.

1 Comment

I'm not talking about your editor/IDE. I'm talking about looking at the code in action -- in the browser.

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.