添加加载图像,而PHP脚本运行 [英] Adding loading image while PHP script runs

查看:141
本文介绍了添加加载图像,而PHP脚本运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP脚本(粘贴下面),从URL的名单拉的元数据,问题是,它可能需要一段时间来加载和用户永远不会知道什么时候它是完全完成(除非他们密切注视的在他们的broswer标签加载图标)

I have a PHP script (pasted below) which pulls in meta data from a list of URL's, the problem is it can take a while to load and the user never knows when it's completely finished (unless they keep an eye on the loading icon in their broswer tab)

我一直在四处寻找网上很长一段时间,但无法找到一个解决办法,我读过,我可以使用Ajax,但我究竟怎么可以用它在这个脚本?

I've been looking around online for a long time but cannot find a solution to this, I've read I can use Ajax but how exactly could I use it on this script?

感谢您的帮助!

<script type="text/javascript">
function showContent(vThis)
{
    // http://www.javascriptjunkie.com
    // alert(vSibling.className + " " + vDef_Key);
    vParent = vThis.parentNode;
    vSibling = vParent.nextSibling;
    while (vSibling.nodeType==3) { // Fix for Mozilla/FireFox Empty Space becomes a TextNode or         Something
        vSibling = vSibling.nextSibling;
    };
    if(vSibling.style.display == "none")
    {
        vThis.src="collapse.gif";
        vThis.alt = "Hide Div";
        vSibling.style.display = "block";
    } else {
        vSibling.style.display = "none";
        vThis.src="expand.gif";
        vThis.alt = "Show Div";
    }
    return;
}

</script>



<form method="POST" action=<?php echo "'".$_SERVER['PHP_SELF']."'";?> >
<textarea name="siteurl" rows="10" cols="50">
<?php //Check if the form has already been submitted and if this is the case, display the   submitted content. If not, display 'http://'.
echo (isset($_POST['siteurl']))?htmlspecialchars($_POST['siteurl']):"http://";?>
</textarea><br>
<input type="submit" value="Submit">
</form>
</div>

<div id="nofloat"></div>
<div style="margin-top:5px;">
<h4><img src="expand.gif" alt="Show Div" border="0" style="margin-right:6px; margin-  top:3px; margin-bottom:-3px; cursor:pointer;" onclick="showContent(this);" />Show me the    script working!</h4>
<div style="margin-top:5px; display:none;">
<table class="metadata" id="metatable_1">
<?php
ini_set('display_errors', 0);
ini_set( 'default_charset', 'UTF-8' );
error_reporting(E_ALL);
//ini_set( "display_errors", 0);
function parseUrl($url){
    //Trim whitespace of the url to ensure proper checking.
    $url = trim($url);
    //Check if a protocol is specified at the beginning of the url. If it's not,     prepend 'http://'.
    if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
            $url = "http://" . $url;
    }
    //Check if '/' is present at the end of the url. If not, append '/'.
    if (substr($url, -1)!=="/"){
            $url .= "/";
    }
    //Return the processed url.
    return $url;
}
//If the form was submitted
if(isset($_POST['siteurl'])){
    //Put every new line as a new entry in the array
    $urls = explode("\n",trim($_POST["siteurl"]));
    //Iterate through urls
    foreach ($urls as $url) {
            //Parse the url to add 'http://' at the beginning or '/' at the end if not   already there, to avoid errors with the get_meta_tags function
            $url = parseUrl($url);
            //Get the meta data for each url
            $tags = get_meta_tags($url);
            //Check to see if the description tag was present and adjust output    accordingly
            $tags = NULL;
$tags = get_meta_tags($url);
if($tags)
echo "<tr><td>$url</td><td>" .$tags['description']. "</td></tr>";
else 
echo "<tr><td>$url</td><td>No Meta Description</td></tr>";
    }
}
?>
</table>
</div>
<script type="text/javascript">
        var exportTable1=new ExportHTMLTable('metatable_1');
    </script>
<div>
        <input type="button" onclick="exportTable1.exportToCSV()"   value="Export to CSV"/>
        <input type="button" onclick="exportTable1.exportToXML()"   value="Export to XML"/>
    </div>
</div>

推荐答案

理念:

加入PHP code到不同的文件

Add php code to different file

显示加载图片

然后再调用这个函数

function Load()
{
    var xmlhttp;
    var url;

    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {           
            //(optional)do something with response: xmlhttp.responseText
            document.getElementById("area").innerHTML=xmlhttp.responseText;
            document.getElementById("loadingimage").src = "afterloading.gif";
        }
    }

    xmlhttp.open("GET","phpfile.php",true);
    xmlhttp.send();
}

此功能去你的JavaScript的地方,那么无论是文件装载完成后,或用jQuery称之为

this function goes to your javascript place then either call it with jquery after document finishes loading or

<body onload="Load()">

和像

<img id="loadingimage" src="loading.gif"/>
<div id="area">...</div>

这篇关于添加加载图像,而PHP脚本运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆