PHP 执行时显示加载图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18927533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:29:52  来源:igfitidea点击:

Show loading image while PHP is executing

phpjquerymysqlmysqlijquery-load

提问by Lmxc

I would like to show loading image while the php script is executing. I have read different answers on how to do that but most of them stated that I should have a separate php page. However I am using single page to show the rows, so how can I be able to show loading image?

我想在 php 脚本执行时显示加载图像。我已经阅读了有关如何执行此操作的不同答案,但其中大多数都表示我应该有一个单独的 php 页面。但是我使用单页来显示行,那么我怎样才能显示加载图像?

Example of select query, I am using to fetch the data:

选择查询的示例,我用来获取数据:

 $stmt = $mydb->prepare("select * from table where firstname = ?  and id = ? ");
 $stmt->bind_param('ss', $firstname, $id);
 $stmt->execute();
 $stmt->close();

回答by TheCarver

In the majority of cases, you wouldhave two pages. The first page, client-side, makes a call to another page, server-side, and shows a pretty spinning thing while it waits. When the server-side page finishes loading (when your query completes) your first page receives a response and then you can hide the pretty spinning thing to let your user know it's finished.

在大多数情况下,你有两页。第一个页面,客户端,调用另一个页面,服务器端,并在等待时显示一个非常旋转的东西。当服务器端页面完成加载(当您的查询完成时)您的第一个页面收到响应,然后您可以隐藏漂亮的旋转内容,让您的用户知道它已完成。

You can use AJAX - in pure Javascript or a lot simpler in jQuery - to dynamically load some data from your PHP page and show a spinning thingy while it waits. I've used jQuery here.

您可以使用 AJAX - 在纯 Javascript 中或在 jQuery 中更简单 - 从您的 PHP 页面动态加载一些数据并在等待时显示旋转的东西。我在这里使用了 jQuery。

CSS

CSS

#loading_spinner { display:none; }

HTML

HTML

<img id="loading_spinner" src="loading-spinner.gif">

<div class="my_update_panel"></div>

jQuery

jQuery

$('#loading_spinner').show();

var post_data = "my_variable="+my_variable;
$.ajax({
    url: 'ajax/my_php_page.php',
    type: 'POST',
    data: post_data,
    dataType: 'html',
    success: function(data) {
        $('.my_update_panel').html(data);
//Moved the hide event so it waits to run until the prior event completes
//It hide the spinner immediately, without waiting, until I moved it here
        $('#loading_spinner').hide();
    },
    error: function() {
        alert("Something went wrong!");
    }
});

PHP(my_php_page.php)

PHP(my_php_page.php)

<?php
// if this page was not called by AJAX, die
if (!$_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') die('Invalid request');

// get variable sent from client-side page
$my_variable = isset($_POST['my_variable']) ? strip_tags($_POST['my_variable']) :null;

//run some queries, printing some kind of result
$SQL = "SELECT * FROM myTable";
// echo results
?>

回答by mszaro

You can't really do this in PHP itself, you'd have to do something in JavaScript for that. So what you would probably want to do is have JQuery show a loading spinner, then execute an AJAX request to your PHP job, and when you get data back, hide the loading indicator.

你不能在 PHP 本身中真正做到这一点,你必须为此在 JavaScript 中做一些事情。因此,您可能想要做的是让 JQuery 显示一个加载微调器,然后对您的 PHP 作业执行 AJAX 请求,当您取回数据时,隐藏加载指示器。