1

ajaxを使用してデータベースにフォームを送信しようとしています。AJAXを使用してデータベースに投稿することに成功しました。しかし、問題は、送信された値がデータベースにすでに存在するかどうかを確認する方法がわからないことです。

私はすでにそうするphpを持っています、しかし私はAJAXを使おうとしています。私はjquery検証プラグインを使用しています以下はコードです。

$('#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."
   }
 },
4

2 に答える 2

1

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;
        }
    }
}
于 2012-07-16T14:34:15.923 に答える
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;
于 2012-07-16T14:36:44.780 に答える