简体   繁体   中英

Ajax response value is always 1?

I have java script in a html page that is sending to vars to a PHP script that then echos one var at the end. The response value is always 1, I have tried alerting, console.log and putting it in a box and nothing changes it.

My 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>

My PHP:

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

echo $workingDays;

?>

UPDATE: I changed the code so it was getting the value from the input instead of inner html but it is still return 1 no matter what.

This:

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

Should be:

// 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);

That is, div1 and div2 should not have quotes around them (you are passing the constant values "div1" and "div2" instead of passing the variable values of vars div1 and div2 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM