Brief analysis on the realization of local refresh function of PHP page _php skills

Source: Internet
Author: User
Tags current time explode modifiers setinterval

There are a lot of ways. The more commonly used is the IFRAME to do so. Now you have Ajax, so it's usually Ajax.
The first method, AJAX implementation:
Of course, the use of Ajax is really very simple can be achieved, but there is a lot of knowledge is somewhat deep. I used to do page time automatic refresh function is the use of Ajax. The complete code is:
1.gettime.php:

Copy Code code as follows:

<?php
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 y-year m-month D-Day", $date-$nowTime);
if ($showtime <= "Beijing Time January 01, 1970 08:00:00") {
echo "Happy New Year";
}
Echo $showtime;

2.zidong.php:
Copy Code code as follows:

<body>
<input type= "button" value= "Start showing Time" id= "Go" onclick= "Start ()"/>
<p> Current time: <font color= "Red" ><span id= "Showtime" ></span></font></p>
</body>
<script type= "Text/javascript" >
var xmlHttp;
function Createxmlhttprequest () {
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);
}
}
}
</script>

In the browser directly access to zidong.php on it, click inside the button can see the effect.
This is a small example of refreshing the local content of a page with Ajax. You might wonder: isn't there any interaction with the database? This is not easy, directly in the gettime.php page inside the operation can be.

There's no need to say much about this method. As for the code inside the Ajax meaning, don't ask me, I said before, this Ajax is still a bit deep.

The second method is implemented using the IFrame method.
Don't tell me to use PHP's include. You go and try it. Can, can not have so many people in Baidu inside ask.
This method is complex to say, in fact, it is quite simple. Let's say the principle:
To refresh the page to automatically refresh the local code out, made a separate page, automatic refresh There are many ways: you can use JavaScript in this independent page to control, what settimeout ("start", 1000); Ah or setinterval ("Start ()", 1000); (Refresh the page every 1 seconds) This can also be done with a META tag: <meta http-equiv= "Refresh" content= "ten" > (Refresh page every 10 seconds). This will be used in the original page with an IFRAME to call it over. That's it.
Let's go to the sample code:
1.show.php:
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<!--<meta http-equiv= "Refresh" content= "5" >-->
<title>Admin</title>
<script language= "javascript" type= "Text/javascript" src= "/extend/js/json.js" ></script>
<script language= "javascript" type= "Text/javascript" src= "/extend/menus.js" ></script>
<script language= "javascript" type= "Text/javascript" src= "/extend/js/jquery-1.4.2.js" ></script>
<link href= "/css/main.css" rel= "stylesheet" type= "Text/css"/>
<link href= "/css/question.css" rel= "stylesheet" type= "Text/css"/>
<script type= "Text/javascript" >
The following Iskeytrigger (), Ctrlenter (), Submitcontent () methods respond to keyboard event submissions. Good compatibility.
function Iskeytrigger (e,keycode) {
var argv = iskeytrigger.arguments;
var argc = isKeyTrigger.arguments.length;
var bCTRL = false;
if (argc > 2) {
bCTRL = argv[2];
}
var Balt = false;
if (argc > 3) {
Balt = argv[3];
}

var nav4 = window. Event? True:false;

if (typeof e = = ' undefined ') {
e = event;
}

if (bCTRL &&
! ((typeof E.ctrlkey!= ' undefined ')?
E.ctrlkey:e.modifiers & event.control_mask > 0)) {
return false;
}
if (Balt &&
! ((typeof E.altkey!= ' undefined ')?
E.altkey:e.modifiers & event.alt_mask > 0)) {
return false;
}
var whichcode = 0;
if (nav4) Whichcode = E.which;
else if (E.type = "KeyPress" | | e.type = = "KeyDown")
Whichcode = E.keycode;
else Whichcode = E.button;

return (Whichcode = = KeyCode);
}

function Ctrlenter (e) {
var ie =navigator.appname== "Microsoft Internet Explorer"? True:false;
if (IE) {
if (Event.ctrlkey && window.event.keycode==13) {
Submitcontent ();
}
}else{
if (Iskeytrigger (e,13,true)) {
Submitcontent ();
}
}
}
function Submitcontent () {
Save_answer ();
}


function Save_answer () {
var $content = $ (' #answer '). Val ();
var $save _answer_url = ' <?php echo $save _answer_url;? > ';
if ($content = = ") {
Alert ("No data!");
Return
}
var $post _data = {
Content: $content,
QID: ' <?php echo $question [' ID '];? > ',
UID: ' <?php echo $questionUser [' ID '];? > '
};
Alert ($save _answer_url);
$.ajax ({
Type: ' Post ',
URL: $save _answer_url,
Data: $post _data,
Success:function (e) {
var $rs = Json.decode (e);
if ($rs. succ = 1) {
Alert ("Answer success!");
$ (' #answer '). Val ("");
Location.reload (); Refresh Page
} else {
Alert ($rs. msg);
}
}
});
}
Delete an Answer
function Deleteanswer ($id) {
var $delete _answer_url = ' <?php echo $delete _answer_url;? > ';
var $post _data = {
ID: $id
};
if (Confirm ("Are you sure delete")) {
$.ajax ({
Type: ' Post ',
URL: $delete _answer_url,
Data: $post _data,
Success:function (e) {
var $rs = Json.decode (e);
if ($rs. succ = 1) {
Alert ("Delete success!");
Location.reload (); Refresh Page
} else {
Alert ($rs. msg);
}
}
});
}
else{
Return
}

}
Set as Best answer
function Setbestanswer ($id, $aid) {
var $set _bestanswer_url = ' <?php echo $set _bestanswer_url;? > ';
var $post _data = {
ID: $id,
Aid: $aid
//  };
if (Confirm ("Are you sure the set this answer is best?") {
$.ajax ({
Type: ' Post ',
URL: $set _bestanswer_url,
Data: $post _data,
Success:function (e) {
var $rs = Json.decode (e);
if ($rs. succ = 1) {
Alert ("Set success!");
Location.reload (); Refresh Page
} else {
Alert ($rs. msg);
//  }
//  }
//  });
//  }
else{
Return
//  }
//
//}
</script>
<!--<script language= "JavaScript" >-->
<!--setinterval ("Myajax ();", 1000);-->
<!--function Myajax ()-->
<!--{-->
<!--//Get information JSON array-->
<!--var html2 = "";-->
<!--HTML2 = "<table id=\" answertable\ "><tr><td class=\" answerhead\ "colspan=\" 2\ "> replied:" +-->
<!--"</td></tr><tr><td><iframe width=0 height=0 src=\" Check_new.php\ "></ Iframe></td></tr> "+-->
<!--"<?php if (isset ($answers) &&!empty ($answers)) {foreach ($answers as $key => $value) {?>" +-->
<!--"<tr><td class=\" boss\ ">" +-->
<!--"<?php echo $value [' Answer '];? > "+-->
<!--"</td><td style=\" text-align:right;\ ">" +-->
<!--"<?php if ($_session[' ADMINISTRATOR ')" {?> "+-->
<!--"<a href=\" Javascript:deleteanswer (<?php echo $value [' ID '];? >); \ ">" +-->
<!--"</a>" +-->
<!--"<?php}?>" +-->
<!--"</td></tr><tr><td class=\" answerauthor\ "colspan=\" 2\ "> Respondents:" +-->
<!--"<?php echo $value [' Email '];? > "+-->
<!--"Answer time:" +-->
<!--"<?php echo $value [' Date '];? > "+-->
<!--"</td></tr><tr><td colspan=\" 2\ "><!--"<?php}}else{?>" +-->
<!--"<tr><td style=\" text-align:center;height:80px;\ "colspan=\" 2\ "> The question is not answered by the user, you can fill in the content below to answer </td></tr> "+-->
<!--"<?php}?> </table>";-->
<!--$ ("#answerDiv"). HTML (HTML2);-->
<!--}-->
<!--</script>-->

<!--<script type= "Text/javascript" >-->
<!--var xmlhttp;-->
<!--function createxmlhttprequest () {-->
<!--if (window. ActiveXObject) {-->
<!--xmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");-->
<!--}-->
<!--else if (window. XMLHttpRequest) {-->
<!--xmlHttp = new XMLHttpRequest ();-->
<!--}-->
<!--}-->
<!--function Start () {-->
<!--//alert ("laslfda;f");-->
<!--createxmlhttprequest ();-->
<!--var url= "/extend/check_new.php?sid=" +math.random () ";-->
<!--//var url = ". /.. /view/product/check_new.php ";-->
<!--//alert (URL);-->
<!--Xmlhttp.open ("Get", Url,true);-->
<!--//alert (URL);-->
<!--Xmlhttp.onreadystatechange = callback;-->
<!--xmlhttp.send (NULL);-->
<!--}-->
<!--function callback () {-->
<!--if (xmlhttp.readystate = 4) {-->
<!--//alert ("Asdflasdf");-->
<!--//if (xmlhttp.status =) {-->
<!--document.getElementById ("Answerdiv"). InnerHTML = xmlhttp.responsetext;-->
<!--//alert (document.getElementById ("Answerdiv"). InnerHTML);-->
<!--settimeout ("Start ()", 1000);-->
<!--//}-->
<!--//alert (xmlhttp.status);-->
<!--}-->
<!--}-->
<!--setinterval ("Start ()", 1000);-->
<!--</script>-->
<body onkeydown= "Javascript:ctrlenter (event);" >
<center>
<div class= "Container" >
<table>
<tr>
<th class= "Zonghead" colspan= "2" > Product questions and answers detailed list </th>
</tr>
<tr>
&LT;TD colspan= "2" ></tr>
<tr>
&LT;TD class= "Questionhead" colspan= "2" > Details of the issue:</td>
</tr>
<?php
if (Isset ($question) &&!empty ($question)) {
?>
<tr>
&LT;TD class= "Questioncontent" colspan= "2" ><?php echo $question [' question '];? ></td>
</tr>
<tr>
&LT;TD class= "Author" colspan= "2" > Questioner: <?php echo $questionUser [' Email ']; > Question Time: <?php echo $question [' Date '];? ></td>
</tr>
<?php
}
?>
<tr>
&LT;TD colspan= "2" ></tr>
</table>
<iframe src= ' <?php echo get_url (Array (' m ' => ' product ', ' a ' => ' product_newmsg ', ' qid ' => $qid));? > "scrolling=" no "frameborder=" 0 "width=" 999px "onload=" this.height= This.contentWindow.document.documentElement.scrollHeight "></iframe>
<!--<div id= "Answerdiv" class= "Answerdiv" >
<table id= "Answertable" >
<tr>
&LT;TD class= "Answerhead" colspan= "2" > Answer:</td>
</tr>
<tr>
<td><iframe width=0 height=0 src= "check_new.php" ></iframe></td>
</tr>
<?php
if (Isset ($answers) &&!empty ($answers)) {
foreach ($answers as $key => $value) {
?>
<tr>
&LT;TD class= "Boss" ><?php echo $value [' Answer '];? ></td>
&LT;TD style= "text-align:right;" >
<?php
if ($_session[' Administrator ']) {//If $_session[' Administrator ']=0 is not a super administrator, the Delete button is not displayed
?>
<a href= "Javascript:deleteanswer (<?php echo $value [' ID '];? >); " ></a>
<?php
}
?>
</td>
</tr>
<tr>
&LT;TD class= "Answerauthor" colspan= "2" > Respondents: <?php echo $value [' Email '];? > Answer time: <?php echo $value [' Date '];? ></td>
</tr>
<tr>
&LT;TD colspan= "2" ></tr>
<?php
}
}else{
?>
<tr>
&LT;TD style= "text-align:center;height:80px" colspan= "2" > The question is currently not answered by the user, you can fill in the content below to answer </td>
</tr>
<?php
}
?>
</table>
</div>
--><table class= "Youwrite" >
<tr>
<td> would you answer that,:</td>?
</tr>
<tr>
<td>
<textarea rows= "cols=" id= "answer" name= "Answer" ></textarea>
</td>
</tr>
<tr>
&LT;TD class= "submits" ><a href= "Javascript:save_answer ();" ></a></td>
</tr>
</table>
</div>
</center>
</body>

2.product_newmsg.php:
Copy Code code as follows:

<meta http-equiv= "Refresh" content= "ten" >
<script language= "javascript" type= "Text/javascript" src= "/extend/js/json.js" ></script>
<script language= "javascript" type= "Text/javascript" src= "/extend/menus.js" ></script>
<script language= "javascript" type= "Text/javascript" src= "/extend/js/jquery-1.4.2.js" ></script>
<link href= "/css/main.css" rel= "stylesheet" type= "Text/css"/>
<link href= "/css/question.css" rel= "stylesheet" type= "Text/css"/>
<script type= "Text/javascript" >
Delete an Answer
function Deleteanswer ($id) {
var $delete _answer_url = ' <?php echo $delete _answer_url;? > ';
var $post _data = {
ID: $id
};
if (Confirm ("Are you sure delete")) {
$.ajax ({
Type: ' Post ',
URL: $delete _answer_url,
Data: $post _data,
Success:function (e) {
var $rs = Json.decode (e);
if ($rs. succ = 1) {
Alert ("Delete success!");
Location.reload (); Refresh Page
} else {
Alert ($rs. msg);
}
}
});
}
else{
Return
}

}
</script>
<div id= "Answerdiv" class= "Answerdiv" >
<table id= "Answertable" >
<tr>
&LT;TD class= "Answerhead" colspan= "2" > Answer:</td>
</tr>
<!--<tr>-->
<!--<td><iframe width=0 height=0 src= "check_new.php" ></iframe></td>-->
<!--</tr>-->
<?php

if (Isset ($answers) &&!empty ($answers)) {
foreach ($answers as $key => $value) {
?>
<tr>
&LT;TD class= "Boss" ><?php echo $value [' Answer '];? ></td>
&LT;TD style= "text-align:right;" >
<?php
if ($_session[' Administrator ']) {//If $_session[' Administrator ']=0 is not a super administrator, the Delete button is not displayed
?>
<a href= "Javascript:deleteanswer (<?php echo $value [' ID '];? >); " ></a>
<?php
}
?>
</td>
</tr>
<tr>
&LT;TD class= "Answerauthor" colspan= "2" > Respondents: <?php echo $value [' Email '];? > Answer time: <?php echo $value [' Date '];? ></td>
</tr>
<tr>
&LT;TD colspan= "2" ></tr>
<?php
}
}else{
?>
<tr>
&LT;TD style= "text-align:center;height:80px" colspan= "2" > The question is currently not answered by the user, you can fill in the content below to answer </td>
</tr>
<?php
}
?>
</table>
</div>


This will be achieved. You can see the effect by accessing the show.php in the browser. But there's a lot of stuff in this example that operates the database. Direct access has no effect. But the code is definitely OK. The principles and code inside are complete.
All right. Here are the two methods. There are two ways that can be done basically all the page local refresh.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.