1

I'm trying to make my table autofresh with ajax after having selected an item from a dropdown list. I'm able to make a selection from mysqldb with a dropdown list in combination with autorefresh. However after autorefresh my selection gets lost so no rows are shown anymore. Obviously i'm doing something wrong. I need some help. Here's part of my code:

making dropdown list + placing 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>

content of file 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);
}
4

0 回答 0