doujujian0052 2019-01-22 21:13
浏览 89
已采纳

如何在不创建2页的情况下向此脚本添加加载gif

I have a simple php script that can take up to 15 seconds to complete. I would like to add a loading gif and I prefer to keep it all on one page

I am a relative noob and I have a php script that performs multiple tasks based on a value that is passed to the script. For example, on a certain form submit, a hidden value "task" with a value of 'getSegments' is passed, and here is some of the php from the getSegments task

if ($task == 'getSegments') {

include('include/pre.content.php');

$response = [];
exec('/usr/bin/phantomjs phantomjstest.js', $response);

$data = $response[19];

// code that parses $data and builds $output

echo $output;

include('include/post.content.php');

}

It can take up to 15 seconds from when the user submits the form until $response is returned and parsed. What is the easiest/best way to add a "loading.gif" to display during this time?

I'd prefer to keep everything in this one php script, with the exception that I can add code to my css files as well as pre.content.php, which contains the opening tags up to and including the closing head tag, and post.content.php, which contains my footer, copyright, and closing body and html tags.

Thanks in advance.

UPDATE 01-22-2019

Per Eriks’ suggestions, I created the following phantomtest.php file to test:

<!DOCTYPE html>
<html>
<head>

<meta charset="utf-8"/>
<meta content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" name="viewport">
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>

</head>
<body>

<img src="images/loading3.gif" id="loading-gif">

<script>
$('#loading-gif').hide();
// $('#task_start_butt').click(function()
$(document).ready(function()
{
    $('#loading-gif').show();
    $.ajax({url:”pathTo getSegments.php"}).done(function(data){
        $('#loading-gif').hide();
        // display the result here
        doSomethingOnComplete(data);
    });
});
</script>

<?php

function doSomethingOnComplete($response)
{
// do here your work
$data = $response[19];
echo "Data = $data";
}

?>

</body>
</html>

and getSegments.php

<?php

$response = [];
exec('/usr/bin/phantomjs phantomjstest.js', $response);
$data = $response[19];
echo $data;

?>

I changed Eriks’ line

$('#task_start_butt').click(function()

to

 $(document).ready(function()

because I want the script to start on page load, when the user comes from the form. I think the script is functioning properly, because loading3.gif is shown for a varying amount of time within the expected range each time phantomtest.php runs, and then it is hidden when the getSegments.php called by ajax finishes (I think). However, I have not been able to display the output from getSegments.php.

If anyone has a suggestion on how to accomplish this I’d be very appreciative. Thanks.


I updated further and moved

function doSomethingOnComplete($response)

out of the php section and put it in the script section. I am now able to output the number 11 with

document.write(5 + 6);

so now all I need is how to get the result from getSegments.php into phantomtest.php.

  • 写回答

2条回答 默认 最新

  • dp20011 2019-01-22 21:35
    关注

    You can call the task with ajax and use JS to show/hide the loading animation.

    HTML/JS (assuming jQuery):

    <a href="#" id="task_start_butt">Start the task</a>
    <img src="loading.gif" id="loading-gif">
    <script>
        $('#loading-gif').hide();
        $('#task_start_butt').click(function()
        {
           $('#loading-gif').show();
           $.ajax({url:"url to your php script"}).done(function(data){
              $('#loading-gif').hide();
              // display the result here
           });
        });
    </script>
    

    you also need to modify your php to return just the data you want to show.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大