【问题标题】:check if email exist in database using jquery validate plugin使用 jquery validate 插件检查数据库中是否存在电子邮件
【发布时间】:2012-07-16 14:32:02
【问题描述】:

我正在尝试使用 ajax 将表单提交到数据库。我已经成功地使用 AJAX 发布到数据库。但问题不是我不确定如何检查提交的值是否已存在于数据库中。

我已经有 php,但我正在尝试使用 AJAX。我正在使用 jquery validate 插件 以下是代码。

$('#subscribeForm').validate({
  errorElement: "p",
  errorPlacement: function(error, element) {
  $('.status').html(error);
},

rules: {
  email: {
    required: true,
    email: true,
    remote: {
        url: 'index.php',
        type: 'post',
        data: {
            email: function(){
            // not sure how to check this
        }
    }
}
}
},

messages: {
email: {
required: "Please enter your email.",
email: "Please enter a valid email.",
// remote: jQuery.format("{0} is already in use")  // doesnt work
}
},

submitHandler: function(form) {
var btn = $(".submit_button");
btn.attr("value", "Submitting..");

$(form).ajaxSubmit({
    success: function(){
        btn.attr("value", "Submitted");
    }
});
return false;
}
});

这是我的 PHP:

if(isset($_REQUEST['subscribeForm'])){
$email = trim($_POST['email']);

if(empty($email) || !valid_email($email)){
    $status = 'Please provide valid email address.';
} else {
    if(email_exist($email)){
        $status = 'You are already subscribed.';
    } else {
        add_email($email);
        $status = 'Thank you for subscribing.';
    }
   }
 } 

编辑

这是我尝试过的,但又失败了。

if(isset($_REQUEST['subscribeForm'])){
$email = trim($_POST['email']);

if(empty($email) || !valid_email($email)){
    $status = 'Please provide valid email address.';
} else {
    if(email_exist($email)){
        $status = 'You are already subscribed.';
        echo false;
    } else {
        add_email($email);
        $status = 'Thank you for subscribing.';
        echo true;
    }
}
} 

还有我的 Javascript

rules: {
email: {
required: true,
email: true,
remote: {
  url: 'index.php',
  type: 'post'
   }
}
},

messages: {
 email: {
required: "Please enter your email.",
email: "Please enter a valid email.",
remote: "You are already subscribed."
   }
 },

【问题讨论】:

    标签: php javascript jquery ajax jquery-validate


    【解决方案1】:

    你应该在你的 JS 中做一些事情来在返回“谢谢订阅”回声时执行成功功能。

    试试;

    Javascript

    messages: {
    email: {
    required: "Please enter your email.",
    email: "Please enter a valid email.",
    success: "Thank you for subscribing.",
    // remote: jQuery.format("{0} is already in use")  // doesnt work
    }
    },
    

    PHP

    if(isset($_REQUEST['subscribeForm'])){
    $email = trim($_POST['email']);
    
    if(empty($email) || !valid_email($email)){
        $status = 'false';
    } else {
        if(email_exist($email)){
            $status = 'false';
        } else {
            add_email($email);
            $status = 'true';
        }
       }
     }
    echo $status;
    

    【讨论】:

    • 我不关心这个消息。我被困在如何检查电子邮件是否已经存在。
    【解决方案2】:

    只需在您的 PHP 文件中回显错误代码并使用 Javascript 中的 AJAX 来解析错误代码并确定电子邮件是否已注册。

    PHP

    define('ERROR_SUCCESS', 0);
    define('ERROR_ALREADY', 1);
    define('ERROR_INVALID', 2);
    
    if (isset($_REQUEST['subscribeForm'])) {
        $email = trim($_POST['email']);
    
        if (empty($email) || !valid_email($email)) {
            $status = 'Please provide valid email address.';
            echo ERROR_INVALID;
        } else {
            if (email_exist($email)) {
                $status = 'You are already subscribed.';
                echo ERROR_ALREADY;
            } else {
                add_email($email);
                $status = 'Thank you for subscribing.';
                echo ERROR_SUCCESS;
            }
        }
    }
    

    【讨论】:

    • 我对 PHP 部分很好。 javascript 是我卡住的地方。
    猜你喜欢
    • 2015-12-09
    • 2013-06-08
    • 2012-03-13
    • 2017-10-09
    • 2013-06-15
    • 2019-07-13
    • 2014-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多