使用下拉列表进行选择后自动刷新mysql表

时间:2012-11-29 21:07:21

标签: mysql ajax drop-down-menu

我正在尝试从下拉列表中选择一个项目后使用ajax使我的表自动刷新。 我可以使用下拉列表和autorefresh从mysqldb中进行选择。然而,在autorefresh之后,我的选择会丢失,因此不再显示任何行。 显然我做错了什么。我需要一些帮助。这是我的代码的一部分:

制作下拉列表+放置div:

<body>
<script src="ajax_aktueel.js"></script>
<script type="text/javascript"><!--
refreshdiv();
// --></script>
<?php
$link1=mysql_connect('pc','user','password');
mysql_select_db('sensordata',$link1);
$query1="select devicemacid, deviceomschrijving from device order by deviceomschrijving asc";
$result1=mysql_query($query1);
echo "<option>Selecteer een device: </option>";
echo "<select name=\"device\" onchange=\"refreshdiv(this.value)\">";
while ($row = mysql_fetch_array($result1))
{
echo "<option value=\"" . $row['devicemacid']  . "\">" . $row['deviceomschrijving']  . "</option>";
}
echo "</select>";
?>
<br />
<br />
<div id="content"></div>
</body>

文件内容ajax_aktueel.js:

var seconds = 1;
var divid = "content";
var url = "file.php";

function refreshdiv(str){

if (str=="")
{
document.getElementById(divid).innerHTML="";
return;
} 
var xmlHttp;
try{
xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
}
catch (e){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
}
catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){
alert("Your browser does not support AJAX.");
return false;
}
}
}

// Timestamp for preventing IE caching the GET request

fetch_unix_timestamp = function()
{
return parseInt(new Date().getTime().toString().substring(0, 10))
}

var timestamp = fetch_unix_timestamp();
var nocacheurl = url+"?idmac="+str+"&t="+timestamp;


xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4){
document.getElementById(divid).innerHTML=xmlHttp.responseText;
setTimeout('refreshdiv()',seconds*1000);
}
}
xmlHttp.open("GET",nocacheurl,true);
xmlHttp.send(null);
}

// Start the refreshing process

var seconds;
window.onload = function startrefresh(){
setTimeout('refreshdiv()',seconds*1000);
}

0 个答案:

没有答案