如何加载Individual Div而不加载整个页面并显示加载状态?

时间:2010-05-18 09:25:14

标签: php mysql ajax

如何在不影响当前页面的情况下单独加载单个Div,并使用PHP和MySQL或Ajax和SQL显示该div的加载状态

2 个答案:

答案 0 :(得分:3)

我这样做:

首先你有一个隐藏的div,如果在里面有一个加载和一个加载按钮:

<div id="displayDiv" style="display: none">
  <img id="loadingGif" src="loadingGif" style="display:none"; />
  <div id="actualContent" style="display:none" />
</div>
<input type="button" id="loadButton" />

然后你有JS代码(我使用jQuery)

<script type="text/javascript">
   $(document).ready( onDocumentReady); // this runs before page load

   function onDocumentReady()
   {
      $('#loadButton').click( onLoadClick ); //assign action on button click
   }   

   function onLoadClick()
   {
       $('#loadingGif').show(); // show the loading gif. It won't show as long as it's parent is hidden
       $('#actualContent').hide(); // hide the actual content of the response;
       $('#displayDiv').show(); // display the div
       $.get("test.php", onRequestComplete ); // make the ajax request to the stand alone PHP file
      //so as long as the content loads, the loading gif will show;
   }

   function onRequestComplete( data )
   {
      $('#loadingGif').hide();
      $('#actualContent').html( data );
      $('#actualContent').show();
   }
</script>

因此。你有一个容器“displayDiv”;在里面你有一个图像“loadingGIf”和另一个容器“actualContent”;单击加载按钮时,会出现带有加载gif的大容器,通知用户正在加载某些内容。加载内容后,您只需隐藏loadingGif,并在“actualContent”gif中显示信息。在test.php中,您只需回显div中必须出现的内容。我建议使用JSON,但你会阅读更多关于它的信息。

希望这有帮助。

答案 1 :(得分:0)

使用PHP + AJAX + Mysql创建一个包含该文件的“.php”文件,调用Ajax函数,其中这个.php(不要给这个名字提供你的文件,这只是一个参考例子)必须调用文件。 。将根据事件调用显示......