[英]Ajax trouble passing multiple form variables for dynamic PHP output
我在傳遞多個變量時遇到問題。 我從這里的基本W3schools示例開始: http : //www.w3schools.com/php/php_ajax_database.asp我的php代碼需要報告類型,該報告類型存儲在“報告”和“ startDate”和“ endDate”中。 與原始樣本和硬編碼的startDate和endDate保持一致時,腳本可以完美執行。 然后,我嘗試將Jason Moon的日歷添加到startDate和endDate,但無法傳遞變量。 理想情況下,我還希望表單在編輯任何字段(包括日歷)時自動更新數據輸出,據我了解,Jason Moon的日歷正在為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],
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.