0

I am making a registering system with jQuery ajax and php. At the beginning of my script i am making two checks. I am checking if the username is too long first and then i am checking if the username is already taken. The php script returns a value when i check if the username is already taken, but doesn't return anything if the username is too long.

Code(PHP):

$usernameR = $_REQUEST['username'];
if($usernameR > 8)
{
    echo "toolong";
}
else
{
    $passwordR = $_REQUEST['password'];
    require_once("dbConn.php");
    $usernameRSF = mysqli_real_escape_string($connect, $usernameR);
    $usernameRSF = htmlspecialchars($usernameRSF);
    $nQuery = mysqli_query($connect, "SELECT * FROM users WHERE username LIKE '%".$usernameRSF."%'") or die("Query error: ".mysqli_error($connect));
    $result = mysqli_num_rows($nQuery);
    if($result > 0)
    {
        echo "taken";
    }
}

Code(JS):

function register()
{
    $(document).ready(function()
    {
        var username = $("#usernameIn").val();
        var password = $("#usernameIn").val();
        if(username != "" && password != "")
        {
            $("#reError").text(" ");
            $("#reError").fadeOut("slow");
            $.ajax({
            url: "../system/register.php",
            type: "POST",
            dataType: "text",
            data: { username: username, password: password },
            success: function(ajaxResult)
            {
                alert(ajaxResult);
                if(ajaxResult == "taken")
                {
                    $("#reError").text("Username already exists");
                    $("#reError").fadeIn("slow");
                }
                if(ajaxResult == "toolong")
                {
                    $("#reError").text("Username too long");
                    $("#reError").fadeIn("slow");
                }
            }
            });
        }
        else
        {
            $("#reError").text("You can't leave username or password empty!");
            $("#reError").fadeIn("slow");
        }
    });
}

3 Answers 3

2

try to change,

$usernameR = $_REQUEST['username'];

to

$usernameR = strlen($_REQUEST['username']);
Sign up to request clarification or add additional context in comments.

Comments

1

You have to compare the length of the string and not the string itself.

strlen

Comments

1

Instead of:

if($usernameR > 8)

Use:

if(strlen($usernameR) > 8)

1 Comment

I know what you mean :)

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.