使用AJAX

时间:2018-09-10 13:58:08

标签: php ajax

我正在尝试更新html表中的许多div。 我从https://www.easy-coding.de/wiki/Entry/64-Mehrere-DIV-Container-mit-AJAX-aktualisieren/中获取了一个示例代码,总体而言,它可以正常工作。

需要更新的每个div都有一个唯一的ID(例如“ s-1”,“ s-2”,“ s-3”,...),并包含以下脚本代码段:

<div id="s-<?php echo $contact_array_row['id']; ?>">initial content</div><script type="text/javascript">up.push('s-<?php echo $contact_array_row['id']; ?>');</script>

这是我修改后的刷新Java脚本:

/**
 * xxx [..] by http://www.easy-coding.de
 * 
 * @param url
 * @param poll      
 */
function UpdateManyDivs(url, poll, tab) {
    this.url = url;
    this.poll = poll ? poll : 750;
    this.list = [];
    this.timer = null;
    this.tab = tab;

    /**
     * adds elem
     * @param   id      string as id
     */
    this.push = function(id) {
        this.list.push(id);
    };

    /**
     * sends single request
     * @param loop  boolean     
     */
    this.fire = function(loop) {
        ajaxPost(this.url + '?' +this.list.join('&'), 'seed='+ new Date().getTime() + '&tab=' + tab, function(up) {
            return function() {
                if (this.readyState == 4 && this.status == 200) {
                    if(loop) {
                        // start timer
                        up.start();
                    }

                    var data = eval('(' + this.responseText + ')');
                    for(var key in data) {
                        document.getElementById(key).innerHTML = data[key];
                    }
                }
            };
        }(this));
    };

    /**
     * stops auto updater
     */
    this.stop = function() {
        window.clearTimeout(this.timer);
    };

    /**
     * starts auto updater
     */
    this.start = function() {
        this.timer = window.setTimeout(function(up) {
            return function() {
                up.fire(true);
            };
        }(this), this.poll);
    };

}

通过以下一线调用:

var up = new UpdateManyDivs('include/callback.php', 1000, event_id);

问题在于,大多数数据都写入$ _GET变量中。 如果包含div的表太大,则它不再起作用。 我想改用$ _POST变量,因此更改了以下行:

ajaxPost(this.url + '?' +this.list.join('&'), 'seed='+ new Date().getTime() + '&tab=' + tab, function(up) {

...对此:

ajaxPost(this.url, '?' +this.list.join('&') + '&seed='+ new Date().getTime() + '&tab=' + tab, function(up) {

在我的回调PHP脚本中,我更改了以下foreach循环:

foreach(array_keys($_GET) as $identifier) {
    $identifier_array = explode("-", $identifier);
    $data[$identifier] = getStatusMYSQL($identifier_array[1],$identifier_array[0],$_POST['tab']);
}

...对此:

foreach(array_keys($_POST) as $identifier) {
    $identifier_array = explode("-", $identifier);
    $data[$identifier] = getStatusMYSQL($identifier_array[1],$identifier_array[0],$_POST['tab']);
}

不幸的是,这无法正常工作,经过几个小时的修复,我没有想法了。

有什么我想念吗?

谢谢, relkai

0 个答案:

没有答案