reload php code with ajax

为君一笑 提交于 2019-12-11 11:39:12

问题


I have php code like this :

<div>
<?php if($link AND $access AND !$timer_expired): ?>
<font color="green">Status: Unlocked - You have successfully unlocked "<?php echo htmlspecialchars($link['link_title']);?>", the website is listed below.</font>
<?php else:?>
<font color="red"> Status : Locked - To Unlock "<?php echo htmlspecialchars($link['link_title']);?>" Please Complete the Survey.</font>
<?php endif;?> 
</div>

I need to let it reload it self until the status be "Unlocked" then stop load

There is many of posts talking about reload php file..but i don't need that..i just want to reload php code

Any idea please ?


回答1:


First of all, if it is PHP page, you'd better to control status on the PHP side. Be independent of client-side. For "Locked" status page set the HTML meta header:

<meta http-equiv="refresh" content="5">

For "Unlocked" status do nothing.

That's all!




回答2:


It's not totally clear what you're asking, but you need to keep in mind that PHP code is executed on the server. AJAX is executed in the client's browser. So, the way to achieve what you want is to use AJAX to continuously reload the HTML in the client's browser, which you would request from a PHP script on the server.

HTML:

<div id="myDiv">AJAX response will load here</div>

Javascript:

function loadPage(your_page)
{
    var xmlhttp;
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
         // This javascript executes when you receive the HTML from your AJAX request
        return xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET",your_page,true);
    xmlhttp.send();

}

var repeat = window.setInterval(checkResponse,3000);

function checkResponse()
{
   var response = loadPage('your_php_script.php');
   if(response.indexOf('Unlocked') !== -1)
       window.clearInterval(repeat);
}



回答3:


you want to use this function and call ajax like this.

<script type="text/javascript">

 jQuery(document).ready(function() {

  setInterval(function(){

   jQuery("#divid").load('http://example.php',
    function(response, status, xhr) {

        if (status == "error") {
        jQuery("#divid").html(msg + xhr.status + " " + xhr.statusText);
        }
    });
  }, 500);

 });
</script>

You also apply condition to check response is 'Locked' or 'Unlocked'.

Hope it helps to you.



来源:https://stackoverflow.com/questions/15697755/reload-php-code-with-ajax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!