Ajax响应值总是1?

时间:2016-03-14 16:10:49

标签: javascript php ajax

我在一个html页面中有java脚本,它将vars发送到PHP脚本,然后在最后回复一个var。响应值始终为1,我已尝试过警报,console.log并将其放在一个框中,没有任何更改。

我的html / JS

<!DOCTYPE html>
<html>
 <head>
     <script language="javascript" type="text/javascript" src="datetimepicker.js"></script>
     <script language="javascript" type="text/javascript" src="datetimepicker2.js"></script>
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
     <link rel="stylesheet" type="text/css" href="css.css">

 </head>
 <body>
     <input id="demo1" type="text" size="25"><a href="javascript:NewCal('demo1','ddmmyyyy')"><img src="cal.gif" width="16" height="16" border="0" alt="Pick a date"></a>
     <input id="demo2" type="text" size="25"><a href="javascript:NewCal2('demo2','ddmmyyyy')"><img src="cal.gif" width="16" height="16" border="0" alt="Pick a date"></a>
     <button onclick="submit()">Click me</button>
     <div id=box></div>
     <script>
      function submit(){
       var div1 = document.getElementById("demo1").value;
       var div2 = document.getElementById("demo2").value;
       var ftest = new Date(div1);
       var ltest = new Date(div2);
          var xhttp = new XMLHttpRequest();
          if(window.XMLHttpRequest){
            xhttp = new XMLHttpRequest();
          }
          else{
               xhttp = new ActiveXObject("Microsoft.XMLHTTP");
          }

       xhttp.open("GET","calc.php?w1="+ftest+"&w2="+ltest,true);
       xhttp.onreadystatechange = function() {
        if(xhttp.readyState==4)   
        {   
            alert(xhttp.responseText); 
        }   
       };
       xhttp.send(null);
      }
     </script>
 </body>   
</html>

我的PHP:

<?php 
// function getWorkingDays($startDate,$endDate,$holidays) {
    $startdate = $_GET['w1'];
    $endDate = $_GET['w2'];
{crapton of math}

echo $workingDays;

?>

更新:我更改了代码,因此它从输入而不是内部html获取值,但无论如何它仍然返回1.

1 个答案:

答案 0 :(得分:2)

此:

var div1 = document.getElementById("demo1").innerHTML;
var div2 = document.getElementById("demo2").innerHTML;
var ftest = new Date("div1");
var ltest = new Date("div2");

应该是:

// Input fields have a value property
var div1 = document.getElementById("demo1").value;
var div2 = document.getElementById("demo2").value;
// Pass that value to date, not the string "div1/2"
var ftest = new Date(div1);
var ltest = new Date(div2);

也就是说,div1div2不应该有引号(你传递的是常数值&#34; div1&#34;和&#34; div2&#34;而不是传递变量div1div2)的变量值。