Ajax结果显示控制台上的空返回

时间:2018-11-06 20:38:30

标签: php jquery ajax

我遇到了这个问题。我使用ajax将值发布到php。脚本工作正常,但是当我使用console.log(result)时,结果显示为空。这是我表单上的日期字段。这是我的输入字段代码:<input type="date" name="bday" class="bday" placeholder="Enter birthday" required="true" />。然后,我将信息发布到执行错误处理的此代码中:
                `

public function validatebday($bday){
                //Check if date is valid
                $valid_date = validateDate($bday);

                if($valid_date != true){
                    return 'Not a valid date!';
                } elseif ($valid_date === true) {
                    //Check if the birthday valid
                    $dob = new DateTime($bday);
                    $age = $dob->diff(new DateTime);
                    $age = $age->y;

                    if($age < 4){
                        return 'You are too young!';
                    }
                }
                return true;
            } //End age check`

我的jquery脚本如下:

//Checking the birthday
self.config.bdayInput.on('blur', function(){
    let bdate = new Date($.trim(self.config.bdayInput.val()));
    let day = ("0" + bdate.getDate()).slice(-2);
    let month = ("0" + (bdate.getMonth() + 1)).slice(-2);
    let bday = bdate.getFullYear()+"-"+(month)+"-"+(day);
    console.log(bday);
    $.ajax({
        data: 'bday=' +bday,
        success: function(result){
            result = $.trim(result);
            console.log(result);
            //Check if the user is old enough
            if(result === 'You are too young!'){
                self.config.bdayerrdiv.addClass('signuperrdiv');
                self.config.bdayerrSpan.addClass('erroroutput').text(result);
                self.config.bdayErr = true;
                return;
            };

            //Checks if the date is a valid date
            if(result === 'Not a valid date!'){
                self.config.bdayerrdiv.addClass('signuperrdiv');
                self.config.bdayerrSpan.addClass('erroroutput').text(result);
                self.config.bdayErr = true;
                return;                        
            };
        }
    });

如果日期不正确,则会出现错误;如果年龄太小,则会出现错误;但是如果两者都通过,则会在控制台上显示此错误:

enter image description here

请问我如何解决此问题的任何想法?

这是我的验证日期的代码:

function validateDate($input_date){

    /*
     * This function does not take in account when the user capture the name of the month instead of the number, eg: Jan, Feb as there are way too many variants for this.  This function is based on the standard format on how the date is stored in Mysql which is yyyy-mm-dd
     */

    //Convert the input date Year, month and day.
    $date = explode('-', $input_date);

    //Split date into YYYY-MM-DD to work with
    $year = (int)$date[0];
    $month = (int)$date[1];
    $day = (int)$date[2];

    //Check is all parameters in the date string is numeric
    if(!is_numeric($year) || !is_numeric($month) || !is_numeric($day)){
        //Return false if any of them is not numeric
        return false;
    }

    //Checks that the year has got 4 digists
    if(strlen($year) < 4){
        return false;
    }

    //Get the leap years from 1700 up to the year that is part of the date string
    //Set initial count
    $count = 0;

    //Set the intervals to determine each leap year.
    $interval = 4;

    //Placeholder to hold leap years.
    $leap_years = array();

        //Loop through the days to determine the leap years.  We start checking from the year 1692 until the year that was entered in the input date.
    for($i = 1692; $i <= $year;$i++ ){
        if($count === $interval){
            $leap_years[] .= $count + $i;
            //Reset count back to 0
            $count = 0;
        }
        //Increment the count.
        $count++;
    }

    //Set the first day of the month
    $first_day_of_month = 1;

    //Determine the last day of the month
    if($month == 1 || $month == 3 || $month == 5 || $month == 7 || $month ==8 || $month == 10 || $month == 12){
        //Set the value for the last day of the month.
        $last_day_of_month = 31;
    }elseif($month == 4 || $month == 6 || $month == 9 || $month == 11){
        //Set the value for the last day of the month.
        $last_day_of_month = 30;
    }elseif($month == 2){
        //Check if the year is a leap year
        if(in_array($year, $leap_years)){
            //Set the last day to 29
            $last_day_of_month = 29;
        }else{
            //Set the last day to 28
            $last_day_of_month = 28;
        }
    }

    //Check the valid first and last days of the month
    if($day < $first_day_of_month){
        //Return false if the day is smaller than 0
        return false;
    }

    //Check if the month if from 1 to 12
    if($month < 1 || $month > 12){
        //Return false of the month is from 1 to 12.
        return false;
    }

    //Check for a valid end date
    if($day > $last_day_of_month){
        //Return false if the day is bigger than the last day of the specific month
        return false;
    }
    //Return true if all the checks passed
    return true;
}

1 个答案:

答案 0 :(得分:0)

如果验证通过,则在PHP脚本中返回true。当通过您的jQuery函数返回此结果时,您不会得到“真实”响应。就是在您的PHP脚本中,而不是返回true,而是返回"valid"(作为字符串),看看它是否出现在您的console.log()中。