Home > Backend Development > PHP Tutorial > A brief analysis of the implementation summary of the partial refresh function of PHP pages_PHP tutorial

A brief analysis of the implementation summary of the partial refresh function of PHP pages_PHP tutorial

WBOY
Release: 2016-07-21 15:04:20
Original
1415 people have browsed it

There are actually quite a few methods. In the past, iframe was more commonly used. Now there is an additional ajax, so ajax is generally used.
The first method, ajax implementation:
Of course, ajax is really simple to use and can be implemented, but a lot of the knowledge in it is still a bit deep. I used ajax to automatically refresh the page time. The complete code is:
1.getTime.php:

Copy the code The code is as follows:

header("cache-control:no-cache,must-revalidate");
header("Content-Type:text/html;charset=utf-8");
$time = "2012-1-20 18:00:00";
$dt_element=explode(" ",$time);
$date_element=explode("-",$dt_element[0]);
$time_element=explode(":",$dt_element[1]);
$date = mktime($time_element[0],$time_element[1],$time_element[2],$date_element[1] ,$date_element[2],$date_element[0]);
$nowTime = time();
$showtime = date("H:i:s, m, month, d, year Y, Beijing time",$date -$nowTime);
if($showtime<="Beijing time, January 1, 1970 08:00:00"){
echo "happy new year";
}
echo $ showtime;

2.zidong.php:
Copy code The code is as follows:



Ajax dynamic display time



Current time:


> if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
}
function start(){
createXMLHttpRequest();
var url="getTime.php";
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange = callback;
xmlHttp.send(null);
}
function callback(){
if(xmlHttp.readyState == 4){
if(xmlHttp. status == 200){
document.getElementById("showtime").innerHTML = xmlHttp.responseText;
setTimeout("start()",1000);
}
}
}




Just access zidong.php directly in the browser, and click the button inside to see the effect.
This is a small example of refreshing partial content of the page using ajax. You may wonder: There is no interaction with the database? This is not easy, just operate it directly in the getTime.php page.
No need to say more about this method. As for what the code in ajax means, don't ask me. I have said before that the ajax in it is still a bit deep.



Second method: Use the iframe method.

Don’t tell me that it’s okay to use PHP’s include. You go and try it. Yes, yes, there won’t be so many people asking in Baidu. This method sounds complicated, but it is actually quite simple. Let’s talk about the principle:
In the page to be refreshed, take out the partial code that needs to be automatically refreshed and make it into an independent page. There are many methods for automatic refresh: it can be controlled with javascript in this independent page. What about setTimeout("start()",1000); or setInterval("start()",1000); (refresh the page every 1 second). In this way, you can also use the meta tag: (refresh the page every 10 seconds). In this way, use iframe to call it in the original page. That's it.
Let’s go to the sample code:

1.show.php:



Copy the code
The code is as follows:

 
 
 
 
 
Admin 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
    
   
    
   
   
    
   
   
    
   
  if (isset($question) && !empty($question)) {
?> 
 
  
 
 
   
 
  }
?> 
   
    
   
  产品问题及回答详细列表

该问题详细内容:
提问者:   提问时间:

 
 
 
    
  
    
    
  
    
    
  
 
 
你也回答一下吧:
 
   
 
 
 
 
 
 

2.product_newmsg.php:
复制代码 代码如下:









    
     
    
     



if (isset($answers) && !empty($answers)) {
 foreach ($answers as $key=>$value){
?>
 


  
  
 
 
 


 

 }
}else{
?>
 
 
 
}
?> 
 
 回答:

    if($_SESSION['ADMINISTRATOR']){//如果$_SESSION['ADMINISTRATOR']=0,即不是超级管理员,则不显示删除按钮
   ?>
   
    }
   ?>
  
回答者:   回答时间:

该问题目前还没有用户回答,你可以在下面填写内容来回答

 


这样就可以实现了。在浏览器中访问show.php就可以看到效果了。但是这个示例里面有很多东西操作了数据库。直接访问没什么效果。但是代码肯定是可以的。里面的原理和代码都是完整的。
好了。这里就介绍这两种方法。有这两种方法已经可以完成基本上所有的页面局部刷新了。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/327762.htmlTechArticle方法其实挺多的。以前比较常用的是iframe这样来做。现在多了个ajax,所以一般情况下都是用的ajax。 第一种方法,ajax实现: 当然,ajax使用...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template