How to display a loading image gif or message while PHP is executing a batch file?

前端 未结 3 2071
无人及你
无人及你 2021-02-01 11:29

I\'ve been searching on the net for the answer, but couldn\'t find.

How could I show some loading messsage or gif while the long executing script is running. I tested a

相关标签:
3条回答
  • 2021-02-01 12:01

    You can't do this using PHP only since is a server-side execution (the page / scripts are rendered in the server before the user can see anything on his browser)

    You should put your script in a separate file, and then do an AJAX call.

    To display the loading image follow this (jQuery framework needed)

    http://jquery-howto.blogspot.com/2009/04/display-loading-gif-image-while-loading.html

    // SHOW YOUR LOADING GIF HERE
    $('#result').load('yourscript.php', function() {
     //HIDE YOUR LODAING GIF HERE
    });
    
    0 讨论(0)
  • 2021-02-01 12:02

    I've tried the following code, which given me exactly what I want. the page is displaying a "loading.gif" while loading the php script, and hide it when the script is finished. This is using JQuery.


    //$tr_arname=$_REQUEST['arname'] -> is the variable i've got from previous PHP page.
    
    <body onload="loadingAjax('myDiv');">
    
    <script>
    var arname="<?php $tr_arname=$_REQUEST['arname']; echo "$tr_arname"; ?>";
    function loadingAjax(div_id)
    {
        $("#"+div_id).html('<center><img src="images/loading.gif"><br><br><font color="#006699" face="arial" size="4"><b>Loading arerror.log <br><?php echo "$tr_arname"; ?> <br>Please Wait ...</b></font></center>');
        $.ajax({
            type: "POST",
            url: "ThePHPScriptPage.php",
            data: "arname=" + arname,
            success: function(msg){
                $("#"+div_id).html(msg);
            }
        });
    }
    </script>
    
    <div id="myDiv"></div>
    </body>
    
    0 讨论(0)
  • 2021-02-01 12:16

    You can request your php file from another web page using AJAX. Then you have your javascript display a loading.gif until the server answers the request.

    You could use jQuery for this: http://api.jquery.com/load/

    0 讨论(0)
提交回复
热议问题