如何在PHP,SQL和JSON响应情况下实施预加载.GIF



任何人都可以建议在以下设置中实现预加载.gif的好方法:

在我的第一页上,我有一个相当长的形式,并带有几个复选框。用户填写表单并单击提交后,将其值存储在数据库中。然后,我将其重定向到另一个具有相当复杂的SQL语句的文件,该文件将用户答案与我的数据库中的几个表进行比较,并将我放入数组中的值列表。然后将此数组传递到包含一些API调用的函数中。您可以想象,整个过程可能需要大量时间,因此我真的很想以动画预加载器的形式向用户提供一些反馈。在线上有很多有关预加载器的信息,但我不确定如何或在哪里做。

为了提供更多细节,我有太多的代码可以在此处发布,但确切的布局如下。

  1. html形式,带有复选框,sql insert语句。插入数据后,我使用标题('位置:x.php')重定向到:

  2. ...带有一系列SQL语句的文件,该文件生成一个带有数据库中一个值的数组。我将该值数组发送到:

  3. ...另一个文件中至少调用两个API(Yelp)并将结果打印为循环的函数。

根本没有任何帮助 - 我什至以最好的方式进行整个过程吗?

谢谢

您不必更改服务器端(.php文件)上的任何内容。实现这一目标所需要做的就是在客户端上使用JavaScript实现Ajax。html/js。

以下是这样做的示例。

html

<!DOCTYPE html>
<html>
<head>
<style>
    .hide { display: none; }
</style>
</head>
<body>
<form>
    <input type="submit">
</form>
<div class="loading hide"><svg width='120px' height='120px' xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid" class="uil-default"><rect x="0" y="0" width="100" height="100" fill="none" class="bk"></rect><rect  x='46.5' y='40' width='7' height='20' rx='5' ry='5' fill='#00b2ff' transform='rotate(0 50 50) translate(0 -30)'>  <animate attributeName='opacity' from='1' to='0' dur='1s' begin='0s' repeatCount='indefinite'/></rect><rect  x='46.5' y='40' width='7' height='20' rx='5' ry='5' fill='#00b2ff' transform='rotate(30 50 50) translate(0 -30)'>  <animate attributeName='opacity' from='1' to='0' dur='1s' begin='0.08333333333333333s' repeatCount='indefinite'/></rect><rect  x='46.5' y='40' width='7' height='20' rx='5' ry='5' fill='#00b2ff' transform='rotate(60 50 50) translate(0 -30)'>  <animate attributeName='opacity' from='1' to='0' dur='1s' begin='0.16666666666666666s' repeatCount='indefinite'/></rect><rect  x='46.5' y='40' width='7' height='20' rx='5' ry='5' fill='#00b2ff' transform='rotate(90 50 50) translate(0 -30)'>  <animate attributeName='opacity' from='1' to='0' dur='1s' begin='0.25s' repeatCount='indefinite'/></rect><rect  x='46.5' y='40' width='7' height='20' rx='5' ry='5' fill='#00b2ff' transform='rotate(120 50 50) translate(0 -30)'>  <animate attributeName='opacity' from='1' to='0' dur='1s' begin='0.3333333333333333s' repeatCount='indefinite'/></rect><rect  x='46.5' y='40' width='7' height='20' rx='5' ry='5' fill='#00b2ff' transform='rotate(150 50 50) translate(0 -30)'>  <animate attributeName='opacity' from='1' to='0' dur='1s' begin='0.4166666666666667s' repeatCount='indefinite'/></rect><rect  x='46.5' y='40' width='7' height='20' rx='5' ry='5' fill='#00b2ff' transform='rotate(180 50 50) translate(0 -30)'>  <animate attributeName='opacity' from='1' to='0' dur='1s' begin='0.5s' repeatCount='indefinite'/></rect><rect  x='46.5' y='40' width='7' height='20' rx='5' ry='5' fill='#00b2ff' transform='rotate(210 50 50) translate(0 -30)'>  <animate attributeName='opacity' from='1' to='0' dur='1s' begin='0.5833333333333334s' repeatCount='indefinite'/></rect><rect  x='46.5' y='40' width='7' height='20' rx='5' ry='5' fill='#00b2ff' transform='rotate(240 50 50) translate(0 -30)'>  <animate attributeName='opacity' from='1' to='0' dur='1s' begin='0.6666666666666666s' repeatCount='indefinite'/></rect><rect  x='46.5' y='40' width='7' height='20' rx='5' ry='5' fill='#00b2ff' transform='rotate(270 50 50) translate(0 -30)'>  <animate attributeName='opacity' from='1' to='0' dur='1s' begin='0.75s' repeatCount='indefinite'/></rect><rect  x='46.5' y='40' width='7' height='20' rx='5' ry='5' fill='#00b2ff' transform='rotate(300 50 50) translate(0 -30)'>  <animate attributeName='opacity' from='1' to='0' dur='1s' begin='0.8333333333333334s' repeatCount='indefinite'/></rect><rect  x='46.5' y='40' width='7' height='20' rx='5' ry='5' fill='#00b2ff' transform='rotate(330 50 50) translate(0 -30)'>  <animate attributeName='opacity' from='1' to='0' dur='1s' begin='0.9166666666666666s' repeatCount='indefinite'/></rect></svg></div>
<div id="result"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
(function($, window, document) {
    $("form").on("submit", function(e) {
        e.preventDefault();
        $("input[type=submit]").prop("disabled", true);
        $(".loading").removeClass("hide");
        $.get("submit.php", function(e) {
            $(".loading").addClass("hide");
            $("#result").html(e);
        });
    });
})(jQuery, window, document);
</script>
</body>
</html>

php

submit.php

<?php
sleep(5);
header('Location: x.php');

X.PHP

<?php
sleep(3);
$arr = [];
for ($i=0; $i<1000; $i++) {
    $arr[] = [
        rand()
    ];
}
echo "<pre>";
var_dump($arr);
echo "</pre>";

最新更新