开发者

Check username JQuery Ajax

I have the following ajax call

 $.ajax(
            {
                type: "POST",
                url: "Utilities/CheckUsername.php",
                data: "un="+ un,
                success: function(data)
                {
                    if(data!=1)
                    {          
                    $('#mike').html(data);
                     return false; 
                    }                   
                }     
            });

I want the page to stay if the username is taken, else redirect to whats in the action attribute of the for.

Obviously this isn't working. But why? I would like to not use preventdefault, so that I could get a better understanding of where the problem and solution is.

EDIT: The server code

<?php
        $seed = 'n48sma94r98';
        $email = $_POST['un'];
        $mysqli = new mysqli('localhost','uml','uml','uml');
        if (!$mysqli) 
        { 
            printf("Can't connect to MySQL Server. Errorcode: %s\n", mysqli_connect_error()); 
            exit; 
        }
        $query = "Select User_Email as Email  from user2 where User_Email = AES_ENCRYPT('$email','$seed') ";
            $result = $mysqli->query($query);
            if($result->num_rows > 0)
            {    
             echo "1";
            }
            else
            {
            echo "2";
            }

?>

The entire form minus the meta:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<link rel="stylesheet" type="text/css" href="my.css">
<script type="text/javascript" src="Resources/js/jquery-1.4.2.min.js"></script>
<script type="text/javascript">$(document).ready(function(){
 $("#login").submit(function() {
        var un  = $('#username').val();       
            $.ajax(
            {
                type: "POST",
                url: "Utilities/CheckUsername.php",
                data: "un="+ un,
                success: function(data)
                {
                    if(data!=1)
                    {          
                    $('#mike').html(data);
                     return false; 
                    }                   
                }     
            开发者_如何学运维});
             }); 
             }); 
             </script>
</head>
<body>
      <form name="login" id="login" method="post" action="dsds.html">
        UserName<input type="text" name="username" id="username"value="">
        Password<input type="text" name="password" id="password" value="">
        Password Again<input type="text" name="passwordagain" id="passwordagain" value="">
        <input type="hidden" name="NewClass" id="NewClass" value="true">
        <input type="submit" name="submit" id="submit" value="submit">
     </form>
     <span id = "mike"></span>
</body>
</html>


Some advice. Name your variables and methods. What is 'un' and 'data'? What do they contain?

Also what type of data is returned? String, HTML, XML, JSON?

If you want to redirect you can use window.location = 'someURL';

To send a form you can do this: $('#form-id').submit();

You could return more meaningful messages, like usernameExists,usernameFound or usernameNotExists,usernameNotFound. Even if you are the only one working on this project, when you go around and ask for help, peeps need to understand your code. It should read like sentences.


I suppose you use

if(data!=1)

to see of the username was taken or not?

Did you try to see the value of data using some sort of a debugger? (or even an alert()).

As I suppose you are not sending a 1 from the server when the username is already taken...


I usually use OK as response indicator (or whatever you call that). If the response (your data) starts with OK it's positive, otherwise the entire message is the error:

In Javascript:

success: function(response) {
    if ( 'OK' == response.substr(0, 2) ) {
        // Okay, so show something green or do nothing
        var message = response.substr(2); // Everything after "OK"
    }
    else {
        // Not okay, so show something red or redirect
        var goto = response;
        // The entire response message `response` is the target destination
        // If you don't want PHP to define the redirect location, just hard code it right here
        window.location = goto;
    }
}

And in PHP:

$result = $mysqli->query($query);
$usernameExists = $result->num_rows > 0; // A nice boolean

exit( $usernameExists ? '/error.html' : 'OKThis username is fine' );

PS. The return false in the success function does nothing. You can omit that.

edit
Without changing the PHP:

success: function(response) {
  if ( '1' !== response ) {
    window.location = 'error.html';
  }
}

Since response is a string, I'd check for that (and not a number). And it never hurts to check for identicallity (===) instead of equality (==).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜