PHP 执行时显示加载图像 [英] Show loading image while PHP is executing

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

问题描述

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

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?

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

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();

推荐答案

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

In the majority of cases, you would have 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.

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

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.

CSS

#loading_spinner { display:none; }

HTML

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

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

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
?>

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

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