Ajax在为动态PHP输出传递多个表单变量时遇到问题


Ajax trouble passing multiple form variables for dynamic PHP output

我有一个问题传递多个变量。我开始与基本的W3schools的例子在这里:http://www.w3schools.com/php/php_ajax_database.asp我的php代码需要类型的报告,这是存储在"报告"&a "startDate" &"endDate"。保持原始样本和硬编码时截止日期,脚本执行完美。然后我尝试添加Jason Moon的日历startDate &在传递变量时遇到麻烦。理想情况下,我还希望表单在编辑任何字段时自动更新数据输出,包括日历,从我的理解那里杰森月亮的日历正在为startDate创建一个隐藏的文本字段;endDate。我有:

    <html>
    <head>
    <script type="text/javascript"> 
    function showReport() 
    { 
        var report = document.getElementById('report').value;
        var startDate = document.getElementById('startDate').value;
        var endDate = document.getElementById('endDate').value;
          if (report=="" && startDate=="" && endDate=="")
          {
            document.getElementById("txtOutput").innerHTML="";
             return;
          }
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("txtOutput").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","report.php?report="+report+"&startDate="+startDate+"&        endDate="+endDate,true);
    xmlhttp.send();
    }
     </script>
     <script type="text/javascript" src="js/cal.js">
    /***********************************************
    * Jason's Date Input Calendar- By Jason Moon http://calendar.moonscript.com/dateinput.cfm
    * Script featured on and available at http://www.dynamicdrive.com
    * Keep this notice intact for use.
    ***********************************************/
    </script>
    </head>
    <body>
    <form>
    <select name="report" onChange="showReport()">
    <option value="">Type:</option>
    <option value="msales">Marketplace</option>
    <option value="lsales">Location</option>
    <option value="cos">COS</option>
    <option value="inventory">Inventory</option>
    </select>
    <br> StartDate:
    <script>DateInput('startDate', true, 'YYYY-MM-DD')</script>
    EndDate:
    <script>DateInput('endDate', true, 'YYYY-MM-DD')</script>
    </form>
    <br>
    <div id="txtOutput"><b>Report info will be listed here.</b></div>
    </body>
    </html>

您遇到的问题是您试图从其id中选择隐藏的输入,但函数DateInput的第一个参数是字段的名称。

所以你必须改变行:

var startDate = document.getElementById('startDate').value;
var endDate = document.getElementById('endDate').value;
由:

var startDate = document.getElementsByName('startDate')[0].value;
var endDate = document.getElementsByName('endDate')[0].value;

如果您有多个相同名称的字段,请注意这可能不会返回您想要的字段,请注意[0],