[英]Getting the latest value from a listbox which has been updated using AJAX
I'm using two listboxes, the 2nd one is updated by AJAX whenever the 1st listbox's value is changed. 我正在使用两个列表框,每当第一个列表框的值更改时,第二个列表框就会由AJAX更新。 Now, based on the values of these two listboxes I need to create a table. 现在,基于这两个列表框的值,我需要创建一个表。 But the problem is that my table is created with the old value of the 2nd listbox and not the latest. 但是问题是我的表是用第二个列表框的旧值创建的,而不是最新的。
Code: 码:
<script>
function pop_to()
{
var xmlhttp= new XMLHttpRequest();
var from=document.getElementById("date_from").value;
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("date_to").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","pop_to.php?from="+from,true);
xmlhttp.send();
}
function update_sales_content()
{
var xmlhttp= new XMLHttpRequest();
var from=document.getElementById("date_from").value;
var to=document.getElementById("date_to").value;
var xmlhttp= new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("sales_data").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","sales_report.php?from="+from+"&to="+to,true);
xmlhttp.send();
}
</script>
<body>
<select id="date_from" onchange='pop_to();update_sales_content();' style="position:relative;left:730px;">
<option>From</option>
<?php
$sql="select distinct order_dt from cust_order order by order_dt";
$result=mysql_query($sql,$con) or die(mysql_error());
if($result)
{
while($rec=mysql_fetch_row($result))
{
echo"<option value='$rec[0]'>$rec[0]</option>";
}
}
?>
</select>
<select id="date_to" onchange='update_sales_content()' style="position:relative;left:800px;">
</select>
<div id="sales_data">
</div>
</body>
AJAX is asynchronous. AJAX是异步的。 When you do: 当您这样做时:
onchange='pop_to(); update_sales_content();'
you're running update_sales_content()
immediately, not waiting for the AJAX response. 您正在立即运行update_sales_content()
,而无需等待AJAX响应。 You need to call it from the pop_to()
callback function: 您需要从pop_to()
回调函数调用它:
function pop_to()
{
var xmlhttp= new XMLHttpRequest();
var from=document.getElementById("date_from").value;
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("date_to").innerHTML=xmlhttp.responseText;
update_sales_content();
}
}
xmlhttp.open("GET","pop_to.php?from="+from,true);
xmlhttp.send();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.