jQuery Validate Remote-检查电子邮件是否已经存在 [英] jQuery Validate Remote - Check if email already exists

查看:100
本文介绍了jQuery Validate Remote-检查电子邮件是否已经存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在获取jQuery验证以检查mysql表中是否已经存在电子邮件地址时遇到了一些问题.

I have a bit of an issue getting jQuery Validation to check if an email address already exists in a mysql table.

每次提交表单时,它都会告诉我该电子邮件已经存在,即使我知道不存在.

Every time I submit the form, it tells me the email already exists, even though I know it doesn't.

这是我的代码:

validation.js

$(document).ready(function () {

    $('#signup').validate({ 
        errorLabelContainer: "#cs-error-note",
        wrapper: "li",
        rules: {
            email: {
                required: true,
                email: true,
                remote: {
                    url: "check-username.php",
                    type: "post"
                }
            }
        },
        messages: {
            email: {
                required: "Please enter your email address.",
                email: "Please enter a valid email address.",
                remote: "Email already in use!"
            }
        },
        submitHandler: function(form) {
            form.submit();
        }
    });

});

check-username.php

<?php
    require('../../private_html/db_connection/connection.php');

    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $query = $conn->prepare("SELECT * FROM 'user_accounts' WHERE email = '" . $_POST['email'] . "'");
    $query->execute();

    if( $query->rowCount() > 0 ){
        echo 'true';
    }
    else{
        echo 'false';
    }
?>

非常感谢您的帮助!

推荐答案

如果查询中存在/else条件,则必须更改行数

You have to change the row count if / else condition in query

脚本

<script>
$(document).ready(function () {
    $('#signup').validate({ 
    errorLabelContainer: "#cs-error-note",
    wrapper: "li",
    rules: {
        email: {
            required: true,
            email: true,
                remote: {
                    url: "check-username.php",
                    type: "post"
                 }
        }
    },
    messages: {
        email: {
            required: "Please enter your email address.",
            email: "Please enter a valid email address.",
            remote: "Email already in use!"
        }
    },
    submitHandler: function(form) {
                        form.submit();
                     }
    });
});
</script>

HTML

<form class="form-inline" role="form" id="signup">
    <div class="form-group">
    <label for="email">Email address:</label>
        <input type="email" class="form-control" name="email" id="email">
    </div>
</form>

PHP

警告请勿使用此PHP代码,因为rowCount()可能无法正常工作,因此请跳过该代码并跳至答案底部的代码.

Warning Do not use this PHP code reason rowCount() may not work so skip it and jump to code at bottom of answer.

<?php
    require('../../private_html/db_connection/connection.php');
    $conn = new PDO("mysql:host=$servername; dbname=$dbname", $username, $password);    
    if(isset($_POST['email'])) {
        $email = $_POST['email'];
        $query = $conn->prepare("SELECT email FROM user_accounts WHERE email = '$email'");
        $query->execute();
        if( $query->rowCount() > 0 ){
            echo 'false';
        } else {
            echo 'true';
        }
    }
?>

由于@Jay Blanchard非常一致,并且不确定上述代码是否有效

As @Jay Blanchard very consistent and dead sure that above code will not work

不,这将不起作用,因为rowCount()对SELECT语句不起作用.您根本就没有行数.

尝试回显$ query-> rowCount(),您会看到问题所在

让我怀疑上面的代码在我的实时服务器上如何工作不应这样,所以我做了一些挖掘并找到了它;

and makes me wonder How the above code is working on my live server when It shouldn't so I done some digging and found this;

如果关联的PDOStatement执行的最后一条SQL语句是SELECT语句,则某些数据库可能返回该语句返回的行数.但是,并非所有数据库都可以保证这种行为,便携式应用程序不应该依赖这种行为.

If the last SQL statement executed by the associated PDOStatement was a SELECT statement, some databases may return the number of rows returned by that statement. However, this behaviour is not guaranteed for all databases and should not be relied on for portable applications.

还有这个

对于大多数数据库,PDOStatement :: rowCount()不会返回受SELECT语句影响的行数.而是使用PDO :: query()发出与所要SELECT语句相同的谓词的SELECT COUNT(*)语句,然后使用PDOStatement :: fetchColumn()检索将返回的行数.然后,您的应用程序可以执行正确的操作.

For most databases, PDOStatement::rowCount() does not return the number of rows affected by a SELECT statement. Instead, use PDO::query() to issue a SELECT COUNT(*) statement with the same predicates as your intended SELECT statement, then use PDOStatement::fetchColumn() to retrieve the number of rows that will be returned. Your application can then perform the correct action.

上述声明的来源php.net手册

在上述两个语句中,一些数据库对于大多数数据库rowCount() 可以工作,但另一方面

In both above statements, some databases and For most databases rowCount() does work but on other-hand

  • 不应依赖于便携式应用程序
  • 使用PDOStatement :: fetchColumn()检索将要行数 被退回.然后,您的应用程序可以执行正确的操作.
  • should not be relied on for portable applications
  • use PDOStatement::fetchColumn() to retrieve the number of rows that will be returned. Your application can then perform the correct action.

由于OP只需要行数而不是所有行的所有数据,因此也可以这样进行.归功于@Jay Blanchard

As OP only wants the count of rows and not all the data of all the rows so can be also done like this. Credit goes to @Jay Blanchard

使用此代码示例

在PHP中进行了一些更改,请使用isset函数.

made some changes in PHP, use isset function.

<?php
    require('../../private_html/db_connection/connection.php');
    $conn = new PDO("mysql:host=$servername; dbname=$dbname", $username, $password);    
    if(isset($_POST['email'])) {
        $email = $_POST['email'];
        $query = $conn->prepare("SELECT email FROM user_accounts WHERE email = '$email'");
        $query->execute();
        $rows = $query->fetchAll();
        $total_rows = count($rows);
            if( $total_rows > 0 ){
                echo 'false';
            } else {
                echo 'true';
            }
    }
?>

查看实际操作

这篇关于jQuery Validate Remote-检查电子邮件是否已经存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆