简体   繁体   English

如何停止使用onload()函数以避免页面刷新时丢失PHP输出?

[英]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的情况下实现相同的效果?

file.js file.js

function showMe(){
    $.ajax({
         type: "POST",
         url: "output.php",  
         cache: false,       
         data: "action=showMessage",        
         success: function(html){                
            $("#container").html(html);  
          }
   });

return false;
}

index.php 的index.php

<!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>

output.php output.php

  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.

相关问题 使用AJAX刷新页面时运行onLoad脚本 - Run onLoad script when page refreshed with AJAX 刷新时如何停止加载功能 - How to stop onload function even when refresh 刷新页面时如何停止页面加载图像 - How to stop page loading image when page is refreshed 刷新页面后,使用javascript构建的计时器会重置吗? 如何避免呢? - Timer built using javascript gets reset when page is refreshed? How to avoid it? 刷新页面时如何阻止复选框被取消选中? - how to stop Checkbox from getting unchecked when page is refreshed? 当使用History.js从应用程序中的其他地方刷新页面时,如何避免呈现浏览会话的初始视图? - How to avoid rendering the initial view of browsing session when page is refreshed from somewhere else in the application using History.js? 即使在 Jquery 中刷新页面,如何保持 setInterval 函数? - How to persist setInterval function even when page is refreshed in Jquery? 刷新页面后,jQuery function 在 PHP 中不起作用 - jQuery function is not working in PHP once the page is refreshed 使用JavaScript刷新页面时如何更改图像? - how to change the image when a page is refreshed using JavaScript? 刷新页面时,阻止Safari显示未保存的表单提示 - Stop safari from showing unsaved form prompt when page is refreshed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM