开发者

Is there a way to auto refresh a variable on a php code without reloading the page?

I have a PHP file with a form and a variable that gets the total numbers of posts from mySQL.

$number shows the result of the SQL query. The total number is useful to me because I use a form and I submit the number also. In the period that I complete the form, the number may change at anytime because a user has submitted a new post during this period. That means the number I send is wrong.

I thought of a solution that reloads a part o开发者_如何学运维f the php code and it will work great for me. I am not a PHP specialist for very advanced solutions, so any ideas and examples are welcome.


Don't submit the number from the form, determine the number in the code that takes the form data. Clearly the result of a SQL query is not form data (unless you can describe why your situation requires otherwise).

If this doesn't answer your question, I think we need much more info and some actual code examples in order to help you.


You are probably looking for an AJAX based solution in PHP.

Update

Here is sample HTML page making AJAX based call to a php script every 10 sec to get refreshed value for your variable. I have intentionally not used jquery etc to demonstrate how AJAX calls are made from a HTML page, this might require some tuning though.

<html>
<head>    
<script type="text/javascript">
var refresh = 10000; // 10 sec; change it if you want
function Ajax(){
    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("No AJAX!?");
             return false;
          }
      }
  }
  xmlHttp.onreadystatechange=function(){
     if(xmlHttp.readyState==4){
        // here you assign fetched value to your input field
        document.myForm.myVar.value = xmlHttp.responseText;
        setTimeout('Ajax()', refresh);
     }
  }
  xmlHttp.open("GET","http://example.com/script_that_refreshes_var.php",true);
  xmlHttp.send(null);
}

window.onload=function() {
    setTimeout('Ajax()', refresh);
}
</script>
</head>
<body>
<form name="myForm">
   <input type="text" name="myVar" value="<?php echo $myVarInitialVal;?>">
</form>
</body>
</html>


Why not simply grab the $number on the processing script, instead of sending it via the form. Two users are filling out the form and both hit submit -> processing script always has an accurate count. Otherwise you get into unnecessary AJAX usage.


What about a Javascript setTimeout() loop, calling an AJAX update function that retrieves the updated post count and fills in the appropriate HTML element? Calling it every 10ish seconds should do it (too often and it will have a tendency to hog slow internet connections).

The exact code will of course depend on the AJAX library used, and the exact page layout.

// Javascript
function startUpdate()
{
   AJAX.call("http://path/to/server/script.php", "updateCallback");
}

function updateCallback(value)
{
    document.getElementById('variableDiv').innerHTML = value;
    setTimeout("startUpdate();", 10000);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜