0 レビュー
2 回答
javascript-phpからの応答を送信する際のクエリ防止フォーム
ユーザーが電子メールがデータベースに存在するというメッセージを受け取った場合、送信ボタンを停止または無効にしようとしています。
私は間にifを入れようとしましたが、運がありませんでした。 jqueryの第一人者はいません。
e.preventDefault();を使用する必要があると言う人もいます。しかし、どこに置くのですか?どこでも試しましたが、スクリプトが機能しないか、ユーザーが登録できます。
たぶん、phpからすべてを検証するのが最善ですか?検証プラグインを使用する場合もjQueryの方が優れていると思っただけです。
手がかりはありますか?ありがとう=)
このスクリプトを持っている:
Jquery:
$("#email").keyup(function (e){
clearTimeout(x_timer);
var e_mail = $(this).val();
if (e_mail.length > 4) {
x_timer = setTimeout(function(){
check_email_ajax(e_mail);
}, 1000);
}
});
function check_email_ajax(e_mail){
$("#email-result").html('<img src="../cms/assets/images/loading.gif" width="20px" height="20px" />');
$.post('ajax/register.ajax.php', {'email':e_mail}, function(data) {
$("#email-result").html(data);
});
}
PHP:
if(isset($_POST["email"])) {
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
die();
}
$email = filter_var($_POST["email"], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW|FILTER_FLAG_STRIP_HIGH);
$statement = $link->prepare("SELECT email FROM FF_Users WHERE email=?");
$statement->bind_param('s', $email);
$statement->execute();
$statement->bind_result($email);
if($statement->fetch()){
die('<i class="fa fa-times-circle" style="color:red;" aria-hidden="true"></i> <small>(taken)</small>');
}else{
die('<i class="fa fa-check" style="color:green;" aria-hidden="true"></i> <small>(Yeey :-) )</small>');
}}
わからない
0
レビュー
答え :
解決策:
button type="button"
を使用すると、送信ボタンを使用せずに完全に作業できます。あなたはこのようなものを探していると思います:
function check_email_ajax(e_mail){
$("#email-result").html('<img src="../cms/assets/images/loading.gif" width="20px" height="20px" />');
$.post('ajax/register.ajax.php', {'email':e_mail}, function(data) {
$("#email-result").html(data);
var result= $("i").attr("id")
if (result==red){
//If this is the result you stay on the page and user see the red message :)
}
if (result==green){
//If result is green I imagine you want to go where the action in form tag says, so delete action from the form, you can simply redirect where you want:
url = "your-file.php"
$(location).attr("href", url)
}
});
}
アイコンのIDを追加します:
if($statement->fetch()){
die('<i class="fa fa-times-circle" id="red" style="color:red;" aria-hidden="true"></i> <small>(taken)</small>');
}else{
die('<i class="fa fa-check" id ="green" style="color:green;" aria-hidden="true"></i> <small>(Yeey :-) )</small>');
}}
お役に立てば幸いです。
わからない
0
レビュー
答え :
解決策:
これが最善の解決策であるかどうかはわかりませんが、正常に機能します。
<input id="submit" type="submit" value="Submit">
メールが見つかった場合:
echo '<script> $(document).ready(function() { $("#submit").attr("disabled", "disabled"); }); </script>';
メールが見つからない場合:
echo '<script> $(document).ready(function() { $("#submit").removeAttr("disabled"); }); </script>';
動作します:-)
わからない
同様の質問
私たちのウェブサイトで同様の質問で答えを見つけてください。