Question

I'm trying to validate an user name with a remote validation.
here is the js code

$('#form_reg').validate({
    rules: {
     username: {
        minlength: 6,
        required: true,
        remote: { url:"validate_usr.php", async:false }
      },
 },
highlight: function(element) {
            $(element).closest('.control-group').removeClass('success').addClass('error');
        },
        success: function(element) {
            element
            .text('OK!').addClass('valid')
            .closest('.control-group').removeClass('error').addClass('success');
        }
  });

and the php file

<?php
 include("bd_conex_mysql.php");

if (!empty($_REQUEST['username']))
{
$usrname = $_REQUEST['username'];
$checkname = mysqli_query("SELECT count(*) FROM cuentas WHERE CtaUsr = '".$usrname."'");  
if($checkname->fetch_row() > 0){
    echo 'false';
}
else{
    echo 'true';
}
}
else
{
 echo 'false';
}

?>

It keeps accepting the input even if the username is already taken.

I've changed the php file to

    echo 'false';
    ?>

and keeps validating the input.

Was it helpful?

Solution

There is no more support for mysql_* functions, they are officially deprecated, no longer maintained and will be removed in the future. You should update your code with PDO or MySQLi to ensure the functionality of your project in the future.

mysql_query returns a resource rather than the result set. So $checkname > 0 will always be true. You need to then fetch with mysql_fetch_assoc

You should change the query command to mysqli_query. And check the results of $checkname->fetch_row() http://us2.php.net/manual/en/mysqli.query.php

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top