首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Php一个页面中的多个调用

Php一个页面中的多个调用
EN

Stack Overflow用户
提问于 2015-03-08 05:53:12
回答 1查看 563关注 0票数 2

现在,我只加载了一个页面(load.php?cid=1,但我想在不同的div中加载5-8 (cid=1、cid=2、cid=3、cid=4、cid=5、cid=6、cid=10 ...etc ))。

我将如何实现这一目标?

代码语言:javascript
复制
$(document).ready(function() {
        function loading_show() {
            $('#loading_Paging').html("<img src='images/loading.gif'/>").fadeIn('fast');
        }

        function loading_hide() {
            $('#loading_Paging').fadeOut 'fast');
        }

        function loadData(page) {
            loading_show();
            $.ajax({
                type: "POST",
                url: "load.php?cid=1",
                data: "page=" + page,
                success: function(msg) {
                    $("#container_Paging").ajaxComplete(function(event, request, settings) {
                        loading_hide();
                        $("#container_Paging").html(msg);
                    });
                }
            });
        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-08 06:55:11

正如JimL所暗示的,我将为页面上的每个元素提供一个公共类,为每个元素提供一个惟一的数据属性(如data-cid="1" ),迭代每个获取cids的元素并为每个元素调用ajax函数。

Id更进一步,使用承诺获取所有ajax响应,然后在承诺被解析后加载所有数据(当所有ajax请求都完成时)。

这是一个有用的例子

HTML:

代码语言:javascript
复制
<div id="loading_Paging"></div>
<div class="myElements" data-cid="1"></div>
<div class="myElements" data-cid="2" ></div>
<div class="myElements" data-cid="3" ></div>
<div class="myElements" data-cid="4" ></div>
<div class="myElements" data-cid="5" ></div>
<div class="myElements" data-cid="6" ></div>
<div class="myElements" data-cid="7"></div>
<div class="myElements" data-cid="8" ></div>

The jQuery:

代码语言:javascript
复制
$(function() {

    var page = 'some string...'
    loadData(page);

    function loadData(){
        $('#loading_Paging').html("<img src='images/loading.gif'/>").fadeIn('fast');
        // loop through each image element
        // calling the ajax function for each and storing the reponses in a `promise`
        var promises = $('.myElements').map(function(index, element) {
            var cid = '&&cid=' + $(this).data('cid'); // get the cid attribute 
            return $.ajax({
                    type: "POST",
                    url: 'load.php',
                    data: "page=" + page +cid, //add the cid info to the post data
                    success: function(msg) {
                    }
            });
        });
        // once all of the ajax calls have returned, te promise is resolved 
        // and the below function is called 
        $.when.apply($, promises).then(function() {
            // arguments[0][0] is first result
            // arguments[1][0] is second result and so on
            for (var i = 0; i < arguments.length; i++) {
                $('.myElements').eq(i).html( arguments[i][0] );
            }
            $('#loading_Paging').fadeOut('fast');
        });
     }
});

我在我的示例中使用的PHP:

代码语言:javascript
复制
<?php
if (isset($_POST['cid']) ){
    // this is just a contrived example
    // in your code youd use the cid to 
    // get whatever data you need for the current div
    echo 'This is returned message '.$_POST['cid'];
}
?>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28923543

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档