簡體   English   中英

jQuery $ .post無法正確執行驗證

[英]jQuery $.post not performing validation properly

我試圖通過將數據發送到檢查有效生日的php文件來使表單檢查有效生日,然后回顯$.post的數據進行處理。

這是我的JavaScript / jQuery:

$('#register_new_user').click(function(evt){
    evt.preventDefault();
    var error_array = new Array();
    var birthday_error_array = new Array();

    // confirm birthday is valid
        var day = $('#day').val();
        var month = $('#month').val();
        var year = $('#year').val();
        var birthday = day + "--" + month + "--" + year;
        $.post('data-test.php?function=birthday',{'birthday':birthday},function(data){
            alert(data);
            if(data != 'valid'){ birthday_error_array.push('error'); error_array.push('error'); }
        });

    // handle birthday validation information
    if(birthday_error_array.length >= 1) {
        $('#birthday_label').css("color","red");
        error_array.push('error');
    } else { $('#birthday_label').css("color","black"); }


    // check for errors if there are errors do not allow the form to be submitted
        if(error_array.length >= 1){
            return false;
        } else { return true; }
});

這是我的php頁面:

session_start();
require 'functions/db_functions.php';
require 'functions/functions.php';
if(isset($_GET['function']) && $_GET['function'] == 'birthday' && isset($_POST['birthday'])){
    $birthday = explode('--',$_POST['birthday']);
    if(!checkdate($birthday[1],$birthday[0],$birthday[2])){
        $result = 'invalid';                    
    } else {$result = 'valid';}
    echo $result;
}

我已經確認我的php和其他頁面正在通信,因為我可以在$.post函數中提醒返回數據有效或無效。 但是,當我嘗試執行上面編寫的if語句時,它不起作用。 它讀取的有效信息無效,等等。我很困惑。

$.post()是異步的,這意味着您提供了一個回調函數,該函數在POST請求完成后將被調用。 在調用回調函數之前先評估if(birthday_error_array.length >= 1)

您需要在回調函數中檢查if(birthday_error_array.length >= 1)

所以像這樣:

$('#register_new_user').click(function(evt){
    evt.preventDefault();
    var error_array = new Array();
    var birthday_error_array = new Array();

    // confirm birthday is valid
    var day = $('#day').val();
    var month = $('#month').val();
    var year = $('#year').val();
    var birthday = day + "--" + month + "--" + year;
    $.post('data-test.php?function=birthday',{'birthday':birthday},function(data){
        alert(data);
        if(data != 'valid'){ birthday_error_array.push('error'); error_array.push('error'); }

        // handle birthday validation information
        if(birthday_error_array.length >= 1) {
            $('#birthday_label').css("color","red");
            error_array.push('error');
        } else { $('#birthday_label').css("color","black"); }
    });
});

我想到了。 我更改為json用於php和javascript之間的通信,並且在ajax函數之后也使用了.done()。 並沒有及時將值添加到錯誤數組以運行檢查。 下面的代碼正常工作:

JQuery:
var day = $('#day').val();
        var month = $('#month').val();
        var year = $('#year').val();
        var birthday = day + "--" + month + "--" + year;
        var birthdayArray = {"birthday": birthday};

        $.ajax({
          type:    "POST",
          dataType: "json",
          url:     'data-test.php?function=birthday',
          data:    birthdayArray,
          success: function(data) {
            if(data["result"] != 'valid') { 
                birthday_error_array.push('error');
                error_array.push('error');
            }
          }
        }).done(function(){
            if(birthday_error_array.length >= 1) {
                $('#birthday_label').css("color","red");
                error_array.push('error');
            } else { $('#birthday_label').css("color","black"); }
        });

PHP:
if(isset($_GET['function']) && $_GET['function'] == 'birthday' && isset($_POST['birthday'])){
    $birthdayArray = array();
    $birthday = explode('--',$_POST['birthday']);
    if(!checkdate($birthday[1],$birthday[0],$birthday[2])){
        $birthdayArray['result'] = "invalid";                   
    } else {$birthdayArray['result'] = "valid";}
    echo json_encode($birthdayArray); // return final result
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM