AJAX调用PHP显示日期

时间:2012-11-14 16:28:02

标签: php ajax

这是我的html / ajax代码

<head>
<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code
function ajaxFunction(){
var ajaxRequest;  // The variable that makes Ajax possible!

try{
    // Opera 8.0+, Firefox, Safari
    ajaxRequest = new XMLHttpRequest();
} catch (e){
    // Internet Explorer Browsers
    try{
        ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try{
            ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e){
            // Something went wrong
            alert("Your browser broke!");
            return false;
        }
    }
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
    if(ajaxRequest.readyState == 4){
        var ajaxDisplay = document.getElementById('ajaxDiv');
        ajaxDisplay.innerHTML = ajaxRequest.responseText;
    }
}
var date = document.getElementById('Date').value;
var queryString = "?date=" + date;
ajaxRequest.open("GET", "php.php" + queryString, true);
ajaxRequest.send(null); 
}

//-->
</script>

<!--showDate AJAX script -->

<!-- //Calender Script -->
<link rel="stylesheet" type="text/css" media="all"   href="scripts/jsDatePick_ltr.min.css"/>

<!--JavaScript-->
<script type="text/javascript" src="scripts/jsDatePick.min.1.3.js"></script>

<!--For javascript Calendar-->  
<script type="text/javascript">
window.onload = function(){
new JsDatePick({useMode:2, target:"Date", cellColorScheme:"orange", dateFormat:"%d-%m-%Y",});};
</script>
</head>
<body>
<form action=""> 
Date &nbsp;:&nbsp; <input type="text" size="20" id="Date" name="Date"/>
<input type="submit" name="submit" value="Submit" onClick="ajaxFunction()"/>
</form>
<div id="ajaxDiv">Time slots will be listed here...</div>

</body>

这是我的PHP代码

<?php

$d = $_GET['date'];
$timestamp = strtotime($_GET['date']);
$date = date("Y-m-d",$timestamp);

echo "Time is $date";
?>

我可以从日历中选择日期,日期将显示在下方。不幸的是,它不起作用。有人请帮助我解决我的问题。我已多次尝试修复错误但仍无法完成。几乎晕了。

2 个答案:

答案 0 :(得分:0)

首先我建议你使用jQuery for Ajax handling,然后为什么在地球上你会查询服务器获取日期,传递日期本身?

使用moment.jsxdate进行客户端日期时间格式化。

答案 1 :(得分:0)

你想做什么以及你在做什么是两件截然不同的事情, 你根本不需要PHP。 2.这是完整的代码供您复制。 http://www.java2s.com/Code/JavaScript/Development/UpdateTimepersecond.htm