如何在不发布表单的情况下检查表单中的重复用户名

时间:2015-07-28 11:30:07

标签: php mysql

我有一份需要电子邮件地址的注册表。我们有重复注册的问题,所以我试图检查我的数据库,看看是否已存在电子邮件地址。检查似乎发生但我总是得到0的回应,我知道这不是真的。

JS:

$(document).ready(function(){ 
$('#Email').blur(check_email); //use keyup,blur, or change 
}); 

function check_email(){
var emailPR = $('#Email').val();
jQuery.ajax({
        type: 'POST',
        url: 'check_email.php',
        data: emailPR,
        cache: false,
        success: function(response){
            if(response == 0){
               alert('Email address available')
            }
            else {
                 alert(emailPR + ' is already registered ')
                 //do perform other actions like displaying error messages etc.,
            }
        }
    });
}

check_email.php:

<?php
/*** mysql hostname ***/
$hostname = 'XXX';

/*** mysql username ***/
$username = 'XXX';

/*** mysql password ***/
$password = 'XXX';

/*** mysql databse ***/
$dbname = 'XXX';

try 
{
    $dbh = new PDO("mysql:host=$hostname;dbname=mysql", $username, $password);
    /*** echo a message saying we have connected ***/
    //echo 'Connected to database';
}
catch(PDOException $e)
{
    echo $e->getMessage();
}   

$stmt = $dbh->prepare("SELECT email FROM clients where email = ?");
$stmt->execute(array($_POST['emailPR']));
echo $stmt->rowCount();
?>

根据您的修正,以下是我的更新代码:

$(document).ready(function(){ 
$('#Email').blur(check_email); //use keyup,blur, or change 
}); 

function check_email(){
var emailPR = $('#Email').val();
$.ajax({
    type: 'POST',
    url: 'check_email.php',
    data: { emailPR: emailPR }, //emailPR this name you get post value. So it must be same name.
    cache: false,
    dataType: 'json',
    success: function(response){
          alert(response); 
        if(response===true)  {
           alert('Email address available')
        }
        else {
             alert(emailPR + ' is already registered ')
             //do perform other actions like displaying error messages etc.,
        }
    }
});
}

email_check.php文件内容现在:

/*** mysql hostname ***/
$hostname = 'xxxxxxx';

/*** mysql username ***/
$username = 'xxxxxx';

/*** mysql password ***/
$password = 'xxxxxxx';

/*** mysql databse ***/
$dbname = 'xxxxxxxx';

try 
{
    $dbh = new PDO("mysql:host=$hostname;dbname=mysql", $username, $password);
    /*** echo a message saying we have connected ***/
    //echo 'Connected to database';
}
catch(PDOException $e)
{
    echo $e->getMessage();
}   

$stmt = $dbh->prepare("SELECT email FROM clients where email = ?");
$stmt->execute(array($_POST['emailPR']));
echo json_encode($stmt->fetchColumn() == 0);

1 个答案:

答案 0 :(得分:1)

完美的工作正常。

还使用数据:{emailPR:emailPR}。您也可以通过这种方式在数据中发送多个参数。试试这个。

        $(document).ready(function(){ 
            $('#customeremail').on('blur', check_email); //use keyup,blur, or change 
        }); 

        function check_email(){
        var emailPR = $('#customeremail').val();
        $.ajax({
                type: 'POST',
                url: 'email_check.php',
                data: { emailPR: emailPR },
                cache: false,
                dataType: 'JSON',
                success: function(response){
                   // alert(response);
                    if(response == 0){
                       alert('Email address available for registration')
                    }
                    else {
                         alert(emailPR + ' is already registered with us')
                         //do perform other actions like displaying error messages etc.,
                    }
                }
            });
        }  

同样检查这个以进行测试,

$emailid = $_POST['emailPR'];

try {
    $dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $stmt = $dbh->prepare("SELECT count(*) FROM clients where email = ?");
    $stmt->execute(array($emailid));
    $number_of_rows = $stmt->fetchColumn(); 
    echo $number_of_rows;

} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

查看结果。

已在DB中注册并可用:

enter image description here

新注册emailid,但在DB中不可用:

enter image description here

希望这有帮助。