dongliang1996 2014-08-17 15:55
浏览 71
已采纳

如何使用PHP AJAX Jquery将javascript警报和消息传递到主页面?

I'm having a problem getting the message using the Ajax jquery. I'm trying to validate the Date. The date should not accept previous dates and accept the dates after 7 days of the current date.

The Main Page is reservation.php

When the condition is not met it should echo a javascript alert in the reservation.php

echo ("<script language='Javascript'> alert('Preferred Date Is Not Allowed!')</script>");

If the condition is met it should echo a message

echo "<img src='img/information.png' style='width:30px;height:30px;'/><i style='color:yellow;'>No. of passengers on " . $format . ": " . $totalpass . '  </i><br>';

This is my Code: Reservation.php

<script type="text/javascript">
$(document).ready(function() {

$("#date").change(function() {
    $.get('getdate.php?parent_cat=' + $(this).val(), function(data) {
        $("#sub_cat2").html(data);
    }); 
});

});
</script>

<input class="form-control datepicker"  name="r_date" id="date" type="text"  placeholder="reservation date">

<div id="subcat2"> </div>

getdate.php

$parent_cat = $_GET['parent_cat'];

if(strtotime($format) <= strtotime(date("Y-m-d",strtotime('+7 days')) ))
{
    echo ("<script language='Javascript'> alert('Preferred Date Is Not Allowed!')</script>");
} 
else
{
    echo "<img src='img/information.png' style='width:30px;height:30px;'/><i    style='color:yellow;'>No. of passengers on " . $format . ": " . $totalpass . '  </i><br>';
}

Kindly help me please Thank you!

  • 写回答

1条回答 默认 最新

  • doutandusegang2961 2014-08-17 16:00
    关注

    Try by changing your scripts something like below

    In getdate.php

    if(strtotime($format) <= strtotime(date("Y-m-d",strtotime('+7 days')) ))
    {
        echo 'invalid';
    }
    

    In Javascript

    $("#date").change(function() {
        $.get('getdate.php?parent_cat=' + $(this).val(), function(data) {
            if(data == 'invalid'){
                alert('Preferred Date Is Not Allowed!');
            }else{
                $("#sub_cat2").html(data);
            }
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究