Jump to content

Update Only When Needed : PHP,Mysql ?


birdbrain24

Recommended Posts

I was wondering if there was anyway that i could only tell the ajax to update when the mysql changes. Heres my script, i was using setTimeout but it really is a cpu hog being played over and over!

 

<?php ...

if($xmlhttp == 'true'){

  $update = $_POST['update'];

  if($update == 'true'){
  
    echo '<strong>Username:</strong> ' . $users['username'] . '  <strong>Rank:</strong> ' . $users['rank'] . '  <strong>Cash:</strong> $' . number_format($users['cash']) . '  <strong>Vehicle:</strong> ' . $selected . '  <strong>Location:</strong> ' . $users['location'] . '  <strong>Reputation:</strong> ' . $users['reputation'];
  
  }

} else {
?>
<!DOCTYPE ...

<head>
<script language="javascript" type="text/javascript">
function UpdateUserbar(){
  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("Your browser do not support our technology!");
             return false;
           }
       }
       }

  xmlhttp.onreadystatechange = function(){
    if(xmlhttp.readyState == 4){
      document.getElementById('userbar').innerHTML = xmlhttp.responseText;
  setTimeout('UpdateUserbar();', 1);
    }
  }
  var params = "xmlhttp=" + "true" + "&" + "update=" + "true";
  var url = "userbar.php";
  xmlhttp.open("POST", url, false);
  xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xmlhttp.send(params); 
}
</script>
</head>
<body onload="UpdateUserbar();">
<div id="userbar"><!-- Userbar Here! --></div>
</body>
</html>
<?php
} 
?>

Link to comment
https://forums.phpfreaks.com/topic/98720-update-only-when-needed-phpmysql/
Share on other sites

  • 3 weeks later...

The problem you would be running into here would be that you would be asking a MySQL & PHP based system to tell a Javascript what to do. This is the problem and cannot be done. at least as far as my knowledge goes. I think you can use Javascript to communicate with MySQL however since JS can be seen via view souce you expose yourself to hackers by releasing database security information.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.