[英]How to stop using onload() function to avoid loosing the PHP output when page refreshed?
I'm using the onload() function to avoid loosing the PHP output when the page is refreshed. 我正在使用onload()函数来避免在刷新页面时丢失PHP输出。 I'm trying to avoid the shaking effects caused by the onload function.
我试图避免上传功能引起的震动效果。 I haven't yet found any alternative way to do that.
我还没有找到任何替代方法。 How can I achieve the same affect without using onload ?
如何在不使用onload的情况下实现相同的效果?
function showMe(){
$.ajax({
type: "POST",
url: "output.php",
cache: false,
data: "action=showMessage",
success: function(html){
$("#container").html(html);
}
});
return false;
}
<!DOCTYPE html>
<head>
<script src="https://code.jquery.com/jquery-3.1.1.js"></script>
<script type="text/javascript" src="file.js"></script>
</head>
<body onload="showMe();">
<div id="container"></div>
<form id="myForm"></form>
</body>
</html>
if ($action=="showMessage")
{
$query="SELECT * FROM `comm` ORDER BY id ASC";
$result = mysqli_query($conn,$query);
if (isset($_REQUEST['parentId'])) {
$parentId = $_REQUEST['parentId'];
}
else { $parentId = 0; }
$i=0; $my = array();
while ($row = mysqli_fetch_row($result)){
$my[$i] = $row; $i++;
}
tree($my, 0, $parentId);
}
function tree($Array, $lvl, $ansId, $pid = 0)
{ $parentId = $ansId;
foreach($Array as $item)
{
if ($item[1] == $pid)
{ ?>
<div style="margin-left:<?php echo($lvl*40); ?>px">
<p><?php echo($item[3]); ?></p>
<?php
if ($lvl<=4){ echo '<a href="javascript:" onclick="OneComment('.$item[0].');">Reply</a>'; }
?> </div> <?php
tree($Array, $lvl+1,$parentId, $item[0]);
}
}
}
?>
One solution is to have the JavaScript code set a cookie and then have the PHP code that renders the HTML check to see if that cookie is set. 一种解决方案是让JavaScript代码设置一个cookie,然后让PHP代码呈现HTML检查以查看是否设置了该cookie。
For example, when the form is submitted (presuming that it calls the showMe()
function and stops the form from submitting manually with .submit() ), set a cookie with document.cookie : 例如,表单提交时(假设它调用
showMe()
函数以及从与手动提交停止形式.submit() ),设置有一个cookie 的document.cookie :
$('#myForm').submit(function(submitEvent) {
//Store cookie, which can be accessed via PHP on next page-load
document.cookie = 'formSubmitted=1';
showMe();
return false;//don't submit form
});
Then when index.php is reloaded, check for that cookie using the superglobal variable $_COOKIE : 然后重新加载index.php时,使用超全局变量$ _COOKIE检查该cookie:
if (isset($_COOKIE['formSubmitted']) && $_COOKIE['formSubmitted']==1) {
//output the messsages (in the div with id="container"), using the code from output.php
//perhaps abstracting that code into a common function that can be used
//in both places
}
See an example of this in this phpfiddle . 在这个phpfiddle中查看此示例 。 Keep in mind that phpFiddle only allows one php script in a fiddle so the code in output.php had to be moved into the top of that PHP script and run only if there was POST data (eg from the AJAX request).
请记住,phpFiddle只允许在一个小提琴中使用一个php脚本,因此output.php中的代码必须移动到该PHP脚本的顶部,并且只有在有POST数据时运行(例如来自AJAX请求)。 Otherwise the code in the files can be kept separate.
否则,文件中的代码可以分开保存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.