Klaus Jasper
Klaus Jasper

Reputation: 89

Ajax - Update div tag on button click

I created a php page that select from a table names, and I have Ajax code that update div tag this is my php code with the name of names.php

<?php
$query = mysql_query("SELECT * FROM `table`");
while($fetch = mysql_fetch_array($query)){
$name = $fetch['name'];
echo $name;
}
?>

and AJAX code update what in the div tag Automatically every 2 seconds

<script type="text/javascript">
function Ajax(){
var xmlHttp;
    try{    
        xmlHttp=new XMLHttpRequest();
    }
    catch (e){
        try{
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); 
        }
        catch (e){
            try{
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e){
                alert("No AJAX!?");
                return false;
            }
        }
    }

xmlHttp.onreadystatechange=function(){
    if(xmlHttp.readyState==4){
        document.getElementById('ReloadThis').innerHTML=xmlHttp.responseText;
        setTimeout('Ajax()',2000);
    }
}
xmlHttp.open("GET","refresh.php",true);
xmlHttp.send(null);
}

window.onload=function(){
    setTimeout('Ajax()',2000);
}
</script>

<div id="ReloadThis">The Names will appear here</div> 

that AJAX code update every 2 sec automatic, but I want to update div tag on click button See Names So how can I do it?

Upvotes: 0

Views: 2076

Answers (1)

Kylie
Kylie

Reputation: 11759

Just add a button...

<button onclick="Ajax();">see names</button>

And takeout this line....

window.onload=function(){setTimeout('Ajax()',2000);}

Upvotes: 2

Related Questions