Check if username exists in database with AJAX

前端 未结 4 1927
遥遥无期
遥遥无期 2020-12-03 09:03

So basically, I want to add a feature to my registration form which I will check if that username already exists in the database.

I have a few questions about AJAX -

4条回答
  •  一个人的身影
    2020-12-03 09:41

    With reading tutorials on the internet, you can learn lots of things. I recommend you to follow the instructions on the following page: http://blog.webwizo.com/2011/05/04/simple-login-with-php-and-jquery-ajax/

    You send the username via post to the specified php file, which searches for the username you have provided.

    Please, use the mysql_real_escape_string function on the input string, so hackers will not be able to use a sql injection attack on your website. It works like this:

    $query = "SELECT * FROM users WHERE username = '".mysql_real_escape_string($_POST['username'])."'";
    if (mysql_num_rows(mysql_query($query)) > 1)
    { 
        print "inuse";
    } 
    

    Then you can check the response value in your ajax jquery function. If the website returns the value "inuse", show an error message that the username is already in use. If not, the username is available.

    But as I've said, please check the tutorial and the most important thing: Use mysql_real_escape_string to prevent sql injection attacks

提交回复
热议问题