1

所以我有一个用户form注册,我想做的是:当用户输入电子邮件时,网站会在他们点击注册按钮之前检查我的数据库是否已经使用了电子邮件。

我遇到的问题是它不会检查。它只会显示“在数据库中搜索”。我只想发布我的代码,这样也许有人能发现我正在犯的错误。

这是我的注册页面的一部分:

<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>

这是我的JavaScript文件,“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('');
    }

});

这是我的php文件,我在其中检查电子邮件“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.';
            }
        }
    }

?>
4

1 回答 1

4

你这里有错误

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

它应该是

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

$_POST['username']应该包含在 isset 函数中

于 2013-08-15T01:24:10.043 回答