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
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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.