创建一个使用 jquery ajax 和 PHP checkdate() 函数验证日期的函数


create a function that validates date using jquery ajax and php checkdate() function

我在Jquery中创建了一个函数,通过使用ajax访问checkdate.php来验证给定的日期。 checkdate.php 使用 checkdate(),如果日期有效,则返回"success"。这是我的jquery函数。

function checkdate(sample)
  {
  $.get('checkdate.php?dateSample='+ sample, function(data){
    if(data == 'success')
      return true;
    else
      return false;
  });
  }

checkdate.php 工作没有错误,我确信这一点,但我的问题是当我使用此代码时 ->

alert(checkdate('2013-09-22'));

alert(checkdate('123123123iuo12'));

它总是提醒"Undefined"

我需要来自检查日期函数的布尔结果。请帮助我。

你可以

试试这个

function checkDate(date)
{
    $.ajax({
        method: 'post', // or GET
        url: 'checkdate.php',
        data: {dateSample: date},
    }).done(function(response){
        alert(response);
        // do stuff ...
    });
}
checkDate('test');