最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - php check if a username exists in a database - Stack Overflow

programmeradmin4浏览0评论

So I have a user form registration, and what I am trying to do is: while a user is typing an email, the website will check my database if the email already has been used or not, before they hit the register button.

The problem I'm having is that it won't check. It will only display "Searching in database". I just want to post my code so maybe someone can catch the error I'm making.

This is part of my registration page:

<tr class = "spacearound"> <!-- input for email address -->
    <th> &emsp;Email: </th>
    <td>
        <input type = "text" id = "user_email" size = "50"
            maxlength = "50" name = "u_email" 
            title = "Enter your email please" 
            onchange = "EmailCheck();" 
            onkeypress = "return InputLimiter(event, 'emailCharacters');"
            /> &#42;
        <span id = "email_status"> </span>
    </td>
    <td><?php  echo $message; ?></td>
</tr>

This is my JavaScript file, "checkusers.js":

$('#user_email').keyup(function() {
    var username = $(this).val();

    $('#email_status').text('Searching database.');

    if(username != ''){
        $.post('checkemail.php',{ username: username }, function(data) {
            $('#email_status').text(data);
        });
    } else {
        $('#email_status').text('');
    }

});

And this is my php file, where I check for an email, "checkemail.php":

<?php
    define('dbHost', 'xxxxx');
    define('dbUser', 'xxxxx');
    define('dbPassword', 'xxxxx');
    define('dbName', 'xxxxx');
    error_reporting(E_ALL ^ E_NOTICE);

    $db = mysqli_connect(dbHost, dbUser, dbPassword, dbName);

    if(mysqli_connect_errno()) { //if connection database fails
        echo("Connection not established ");
    }  //by now we have connection to the database

    if(isset($_POST))['username'])){ //if we get the name succesfully
    $username = mysqli_real_escape_string($db, $_POST['username']);
        if (!empty($username)) {
            $username_query = mysqli_query($db, "SELECT COUNT(`firstName`) FROM `users` WHERE `email`='$username'");    

        $username_result = mysqli_fetch_row($username_query);

            if ($username_result[0] == '0') {
                echo 'Email available!';
            } else {
                echo 'Sorry, the email '.$username.' is taken.';
            }
        }
    }

?>

So I have a user form registration, and what I am trying to do is: while a user is typing an email, the website will check my database if the email already has been used or not, before they hit the register button.

The problem I'm having is that it won't check. It will only display "Searching in database". I just want to post my code so maybe someone can catch the error I'm making.

This is part of my registration page:

<tr class = "spacearound"> <!-- input for email address -->
    <th> &emsp;Email: </th>
    <td>
        <input type = "text" id = "user_email" size = "50"
            maxlength = "50" name = "u_email" 
            title = "Enter your email please" 
            onchange = "EmailCheck();" 
            onkeypress = "return InputLimiter(event, 'emailCharacters');"
            /> &#42;
        <span id = "email_status"> </span>
    </td>
    <td><?php  echo $message; ?></td>
</tr>

This is my JavaScript file, "checkusers.js":

$('#user_email').keyup(function() {
    var username = $(this).val();

    $('#email_status').text('Searching database.');

    if(username != ''){
        $.post('checkemail.php',{ username: username }, function(data) {
            $('#email_status').text(data);
        });
    } else {
        $('#email_status').text('');
    }

});

And this is my php file, where I check for an email, "checkemail.php":

<?php
    define('dbHost', 'xxxxx');
    define('dbUser', 'xxxxx');
    define('dbPassword', 'xxxxx');
    define('dbName', 'xxxxx');
    error_reporting(E_ALL ^ E_NOTICE);

    $db = mysqli_connect(dbHost, dbUser, dbPassword, dbName);

    if(mysqli_connect_errno()) { //if connection database fails
        echo("Connection not established ");
    }  //by now we have connection to the database

    if(isset($_POST))['username'])){ //if we get the name succesfully
    $username = mysqli_real_escape_string($db, $_POST['username']);
        if (!empty($username)) {
            $username_query = mysqli_query($db, "SELECT COUNT(`firstName`) FROM `users` WHERE `email`='$username'");    

        $username_result = mysqli_fetch_row($username_query);

            if ($username_result[0] == '0') {
                echo 'Email available!';
            } else {
                echo 'Sorry, the email '.$username.' is taken.';
            }
        }
    }

?>
Share Improve this question edited Aug 15, 2013 at 4:39 Nate 31k13 gold badges85 silver badges209 bronze badges asked Aug 15, 2013 at 1:15 Some userSome user 492 gold badges3 silver badges5 bronze badges 5
  • What does "won't check" mean? Do you see the POST request in your browser Developer Tools Network panel? Does it return 200, or an error code? Is there anything in your web server logs? – Amadan Commented Aug 15, 2013 at 1:18
  • Try using firbug to check the AJAX request status or change it GET in JS and PHP and test that PHP in the browser like this checkemail.php?username=something – Michael B. Commented Aug 15, 2013 at 1:18
  • have you tried directly calling your PHP script? You might need to change $_POST to $_REQUEST for this and then fire up your browser checkemail.php?username=foo to narrow down the problem. – devsnd Commented Aug 15, 2013 at 1:19
  • @ i get a server error when i fire up checkemail.php on my browser – Some user Commented Aug 15, 2013 at 1:24
  • This question appears to be off-topic because it is fixed by correcting a typo. – Daedalus Commented Aug 15, 2013 at 1:30
Add a ment  | 

1 Answer 1

Reset to default 4

you have error here

if(isset($_POST))['username'])){

it should be

if(isset($_POST['username'])){

$_POST['username'] should be enclosed inside isset function

发布评论

评论列表(0)

  1. 暂无评论