如何使用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!

dsghpgmay31938863
dsghpgmay31938863 显示的数据是正确的先生,我如何在reservation.php中回显它
大约 6 年之前 回复
dp20011
dp20011 在$.get之后的Reservation.php上('getdate.php?parent_cat='+$(this).val(),function(data){尝试添加警报(数据);查看从服务器返回的响应。
大约 6 年之前 回复
doushifang4382
doushifang4382 我如何回应reservation.php中的消息?谢谢你,先生
大约 6 年之前 回复
dqw7121
dqw7121 问题是什么?
大约 6 年之前 回复

1个回答

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);
        }
    });
});
douyan1903
douyan1903 先生,我尝试了代码,但是如果它通过了条件,它就没有显示警报和消息。 对不起,先生,我对JS不好
大约 6 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐