Lets say I have a php file where I can fetch content from sql using links like: then it will fetch all username's content. That not a problem..but now I want to do a timed auto refresh without refreshing the page. The content is displayed in a div. the php code is <div> <?php ..my fetch content scripts here.. ?> </div> PHP: I want to refresh the content inside the div only. I think I can do it using JS or Ajax right? but how? I did some web search but i cant find the solution. maybe i use wrong keyword or what? Thank you.
are you familiar with ajax? on the js side just put a t=setTimeout(ajaxfunction(),1000); and it will loop again based on the second parameter (time in milliseconds) and reload the same function.. and thats it
Nope i am not familiar with ajax. My normal JS is quite ok compared to Ajax. I can do the settimeout thing. But now I need the Ajax part which send query to the php file and update the content. Anyway, thank you. =D +rep
i can post you some basic ajax code that will auto refresh the server side (i mean the page where your query goes in.)
function refreshVal(uname) { var xmlHttp; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { // Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { alert("Your browser does not support AJAX!"); return false; } } } xmlHttp.onreadystatechange=function() { if(xmlHttp.readyState==4) { document.getElementById('required_div').innerHTML = xmlHttp.responseText; } } xmlHttp.open("GET","thefilewithphpcode.php?username="+uname,true); xmlHttp.send(null); } Code (markup): Now thefilewithphpcode is the file that has your phpcode. So every few seconds, which you can do by using setTimeout('refreshVal("'+document.getElementById('uname').value+'");',refreshInterval); Again this script may need some working so as to get the value of uname correctly but most of the cases, it should work. Thanks imphpguru