Jojo01
Jojo01

Reputation: 1289

jQuery ajax php returns nothing

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");
        }
    });
}

Upvotes: 0

Views: 65

Answers (3)

user4628565
user4628565

Reputation:

try to change,

$usernameR = $_REQUEST['username'];

to

$usernameR = strlen($_REQUEST['username']);

Upvotes: 2

Jeremy Board
Jeremy Board

Reputation: 181

Instead of:

if($usernameR > 8)

Use:

if(strlen($usernameR) > 8)

Upvotes: 1

Pradeep
Pradeep

Reputation: 2507

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

strlen

Upvotes: 1

Related Questions