AJAX和PHP无法正常运行

时间:2012-11-06 12:00:34

标签: php html ajax

这是我的代码。

<html>
<head>
<title>Patient Information Management</title>
<!--showDate AJAX script -->
<script language="javascript" type="text/javascript">
function showDate(){
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 and will update
// div section in the same page.
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
  var ajaxDisplay = document.getElementById('ajaxDiv');
  ajaxDisplay.value = ajaxRequest.responseText;
 }
 }
// Now get the value from user and pass it to
// server script.
var date = document.getElementByName('Date').value;
var queryString = "?date=" +date ;
ajaxRequest.open("GET", "getappointmentdate.php" +queryString, true);
ajaxRequest.send(null); 
}
</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>

<div class="wrapper">
<?php include("include/header.php"); ?>
<div class="clear"></div>
<?php 


<div class="clear"></div>



</div> <!-- end of sidemenu div -->
</div> <!-- end of left div -->

<div id="right" >

<h2>anything</h2>
<div class='grey_divider'></div>

<p>&nbsp;</p>
<div class='grey_divider'></div>
<h3>Make Appointment</h3>
<form action=""> 
 Date &nbsp;:&nbsp; <input type="text" size="20" id="Date"  name="Date"/>
 <input type='button' onclick='showDate()' value='Submit'/>
 </br>
 </br>

 </form>


</div>
</body>
</div>
</html>

当我点击“提交”按钮时,它应该运行php文件,但不幸的是,什么都没发生,有人可以帮我。 我用getElementByName而不是getElementById捕获html日期,因为'id'用于捕获日历js。它会影响吗?

1 个答案:

答案 0 :(得分:0)

我很确定你需要抓住按钮点击并使用.preventDefault()停止它的默认行为,然后调用你的函数。

添加了代码

$('input[type="submit"]').click(function(e) {
  e.preventDefault();
  showDate();
});