使用ajax检查电子邮件是否存在

时间:2016-09-22 16:14:55

标签: javascript php ajax

基本上,我想自动检查数据库中是否存在电子邮件。 connection.php&已插入mysqli个查询。

HTML:

<input type="email" name="email" class="formEmail">

JS:

  $( document ).ready(function() {
    $('.formEmail').on('change', function() {
        //ajax request
        $.ajax({
            url: "queries/checkEmail.php",
            data: {
                'email' : $('.formEmail').val()
            },
            dataType: 'json',
            success: function(data) {
                if(date == true) {
                    alert('Email exists!');
                }
                else {
                    alert('Email doesnt!');
                }
            },
            error: function(data){
                //error
            }
        });
    });
  });

PHP:

require_once("../connection.php");

$userEmail = $_GET['email'];

$checkEmail=mysqli_query($con, "SELECT email from accounts WHERE email='$userEmail'");

if (mysqli_num_rows($checkEmail) == 1) {
  $response = true;
} else {
  $response = false;
}

echo json_encode($response);

3 个答案:

答案 0 :(得分:1)

select c.consumerid, signupdate, state, year(DOB), topaffiliate, activestatus, lastuseddate, IFNULL(Total_ACH, 0) AS Total_ACH, IFNULL(Bill_Pay, 0) AS Bill_Pay, IFNULL(Revenue, 0) AS Revenue, IFNULL(Cash__TXNs, 0) AS Cash__TXNs, ... FROM consumerdata AS c LEFT JOIN ( SELECT consumerid, SUM(transactionamount) AS Total_ACH FROM achload GROUP BY consumerid) AS a ON a.consumerid = c.consumerid LEFT JOIN ( SELECT consumerid, SUM(transactionamount) AS Bill_Pay FROM billpay GROUP BY consumerid) AS b ON b.consumerid = c.consumerid LEFT JOIN ( SELECT consumerid, SUM(transactionamount) AS Revenue FROM recharge GROUP BY consumerid) AS r ON r.consumerid = c.consumerid LEFT JOIN ( SELECT consumerid, COUNT(*) AS Cash__TXNs FROM Cash GROUP BY consumerid) AS cash ON cash.consumerid = c.consumerid ... WHERE c.signupdate between DATE(20120101) and CURDATE() ORDER BY signupdate ASC 调用函数的成功块中,if条件中的响应得到了一个小错误。

  

在条件

中将ajax重写为date
data

答案 1 :(得分:0)

当您使用ajax并且输出是JSON时,您只需要发送纯JSON输出,请检查以下代码。我在checkEmail.php文件中添加了JSON消息的头文件,因此可以通过javascript轻松读取

<强>代码

<?php
require_once("../connection.php");

$userEmail = $_GET['email'];

$checkEmail=mysqli_query($con, "SELECT email from accounts WHERE email='$userEmail'");

if (mysqli_num_rows($checkEmail) == 1) {
  $response = true;
} else {
  $response = false;
}

//Here your required to set the header which will make output as pure JSON output and easy to read by javascript

header('Content-Type:application/json;');
echo json_encode($response);

答案 2 :(得分:0)

我想你忘了在Ajax中添加请求方法:type:'post' 当然,在PHP中修复:将GET更改为POST