使用ajax、php和mysql进行表单验证

时间:2022-09-26 07:47:02

I'm building a login form and want to instantly check to make sure the email address is one that's in my database.

我正在构建一个登录表单,并希望立即检查,以确保电子邮件地址是我的数据库中的一个。

I've looked around and tried a bunch of different things to make this work, but I can't seem to get this working correctly.

我环顾四周,尝试了很多不同的东西来让这个工作,但是我似乎不能让这个工作正确。

Here's the Form

这是形式

<form>
<input type="text" id="email" name="email" placeholder="Email Address" onblur="checkEmail(this.value);" onchange="checkEmail(this.value);"/>
<input type="password" id="password" name="password" placeholder="Password"/>
<input type="button" id="login-signup" value="Login / Sign Up"/>
</form>

Here's the Javascript. This is where I think the problem is.

这是Javascript。这就是我认为的问题所在。

function checkEmail(email) {
    // check to see if the email exists in the database using PHP and MySQL
    $.ajax({                                      
        url: 'login.php',               //the script to call to get data 
        type: 'post',  
        data: {
            email: $('#email').val()
        }, 
        dataType: 'json',               //data format      
        success: function(response) {       //on reception of reply
            if(response == 'match') {
                console.log('match');
            } else if (response == 'no match') {
                console.log('no match');
            } else if (response == 'error') {
                console.log('error');
            } else {
                console.log('who knows');
            }
        } 
    });

}

And here's login.php If I navigate to mywebsite.com/login.php?email=email then everything works correctly, so I know this is doing what I need it to. I imagine the problem is in my ajax

这是登录。php导航到mywebsite.com/login.php?电子邮件=电子邮件那么一切都正常工作,所以我知道这是在做我需要它做的。我想问题出在我的ajax上

$db_host = "host";
$db_user = "user";
$db_pass = "pass";
$db_name = "name";


$db = new PDO('mysql:host='.$db_host.';dbname='.$db_name, $db_user, $db_pass);


if(isset($_GET['email'])) {
    $email = $_GET['email'];
    echo $email;
}

// Using prepared statements almost eliminates the possibility of SQL Injection.
$preparedQuery = $db->prepare("SELECT * FROM `members` WHERE `email` = :email");
$preparedQuery->bindValue(":email", $email);
$preparedQuery->execute();

// Retrieve the results from the database
$user = $preparedQuery->fetch(PDO::FETCH_ASSOC);

// If there is a user record print the user & pass... 
if($user != ''){
    echo 'match';
    $_SESSION['email'] = $email;
} else if ($user == '') {
    echo 'no match';
} else {
    echo 'error';
}

2 个解决方案

#1


2  

@user4035 suggested removing the data type from the AJAX request, and that got the request to return results. @Banik suggested changing $_GET to $_POST and that worked as well. Now the entire thing works perfectly

@user4035建议从AJAX请求中删除数据类型,这使请求返回结果。@Banik建议将$_GET改为$_POST,这也起了作用。现在整个过程都很顺利

#2


1  

If you're trying to send a json object, you'll need to read the raw data.

如果要发送json对象,需要读取原始数据。

$request_body = file_get_contents('php://input');
$email = json_decode($request_body, true);

Then you'll have the email data in an array. You could also remove the datatype in the ajax call and read the data in $_POST['email']. You're sending in post data and trying to access it in $_GET

然后你会有一个数组中的电子邮件数据。您还可以删除ajax调用中的数据类型,并读取$_POST['email']中的数据。您正在发送post数据并试图在$_GET中访问它

#1


2  

@user4035 suggested removing the data type from the AJAX request, and that got the request to return results. @Banik suggested changing $_GET to $_POST and that worked as well. Now the entire thing works perfectly

@user4035建议从AJAX请求中删除数据类型,这使请求返回结果。@Banik建议将$_GET改为$_POST,这也起了作用。现在整个过程都很顺利

#2


1  

If you're trying to send a json object, you'll need to read the raw data.

如果要发送json对象,需要读取原始数据。

$request_body = file_get_contents('php://input');
$email = json_decode($request_body, true);

Then you'll have the email data in an array. You could also remove the datatype in the ajax call and read the data in $_POST['email']. You're sending in post data and trying to access it in $_GET

然后你会有一个数组中的电子邮件数据。您还可以删除ajax调用中的数据类型,并读取$_POST['email']中的数据。您正在发送post数据并试图在$_GET中访问它