使用php和jquery的Progressbar

时间:2016-11-01 16:44:52

标签: php jquery ajax progress-bar

我想制作一个邮件发送程序,并在等待时通过进度条通知用户。不幸的是,它没有按预期工作,进度条没有更新。

程序循环遍历从数据库派生的一组邮件地址。首先将验证邮箱中是否存在邮件地址。如果没有,将报告。收集报告,最后发送回浏览器。

每秒通过javascript函数“mitgl.progressBar”单独的ajax帖子报告进度,并由服务器通过php底部的函数“getProgress”发送。

邮件验证和邮件发送有效,但getProgress似乎只做了一次。

尽管附加的代码只是一个片段,但其余的代码工作正常。

我找不到问题,也许有人可以看到我对...的看法......

使用Javascript:

versandMail: function() {
    mitgl.unselectRec();
    mitgl.pInt = window.setInterval(mitgl.progressBar, 1000);
    var oForm = $('form[name=vs]').get(0);
    $.post(location.href, {
        cmd: 'M mailVersand',
        de: oForm.de.value,
        fr: oForm.fr.value,
        sr: oForm.sr.value,
        aktiv: oForm.aktiv.value,
        anfragen: oForm.anfragen.value,
        vorstand: oForm.vorstand.value,
        idList: (oForm.idList ? oForm.idList.value : ''),
        betreff: oForm.betreff.value,
        mailtext: $('textarea[name=mailtext]', oForm).htmlarea('html'),
        attachments: JSON.stringify(mitgl.oVersand.mail.attachments)
    }, function(data, status, oXhr){
        window.clearInterval(mitgl.pInt);
        $('#progressbar').remove();
        $('#mailReport').remove();
        if (data.isEmpty()) {
            window.alert('Auswahl hat keine Adressen ergeben');
        } else if (data.substr(0, 6) === 'Fehler') {
            window.alert(data);
        } else {
            $('#protokoll tbody').html(data);
            mitgl.protoLink();
            mitgl.selectTop();
        }
    });
},
progressBar: function() {
    $.post(location.href, {
        cmd: 'M getProgress'
    }, function(nProgress) {
        if ($('#progressbar').length > 0) {
            $('#progressbar .bar').css({width: nProgress+'%'});
        } else {
            var pb = $('<div/>')
                    .attr('id', 'progressbar')
                    .appendTo('#cmd');
            $('<div/>')
                    .addClass('bar')
                    .appendTo(pb);
        }                   
    });
},

PHP:

function mailVersand() {
    // ... Prepare Mail Data ...

    require_once 'phpmailer.class.php';
    require_once('class.smtp.php');
    require_once('class.verifyEmail.php');

    $oVerify = new verifyEmail();
    $oVerify->setEmailFrom($cMailFrom);

    $oMail = new PHPMailer();
    $oMail->SMTPDebug = 0;
    $oMail->IsSMTP();  // telling the class to use SMTP
    //
    //  ... and so on ...
    $oMail->Host = ...

    $aErrors = [];
    $nSent = 0;
    $nError = 0;
    $nProcessed = 0;
    $nMails = count($aMitglied);

    session_start();               // <-- Session starts
    $_SESSION['nProgress'] = '0';  // progress is zero

    // loop through mailing list
    foreach ($aMitglied as $r) {
        $aEmail = explode(';', $r->email);
        $email = $aEmail[0];
        if ($oVerify->check($email)) {
            $oMail->AddAddress($email,"$r->vorname $r->name");

            // mail verificatio is ok, try to send
            if ($oMail->send() === TRUE) {
                $nSent++;
            } else {
                // no, report error
                $e = new stdClass();
                $e->email = $email;
                $e->name = $r->name;
                $e->vorname = $r->vorname;
                $e->error = $oMail->ErrorInfo;
                $aErrors[] = $e;
                $nError++;
            }*/
            $oMail->ClearAddresses();
        } else {
            // Mail verification failed, report error
            $e = new stdClass();
            $e->email = $r->email;
            $e->name = $r->name;
            $e->vorname = $r->vorname;
            $e->error = $oVerify->getAllErrors();
            $aErrors[] = $e;
            $nError++;
        }
        $nProcessed++;        // <-- Next processed record
                              // v-- Calulate percentage of progress
        $_SESSION['nProgress'] = strval(round($nProcessed *100 /$nMails));
    }

    // create error report
    $oBericht = new stdClass();
    $oBericht->sent = $nSent;
    $oBericht->error = $nError;
    $oBericht->fails = $aErrors;

    // now procedure finished, reply final report
    // ....
    $s = $this->listVersand();
    echo ($s);  // send reply
    session_write_close(); // session ends
    exit;
}

function getProgress() {
    session_start();
    //$n = isset($_SESSION['nProgress']) ? "$_SESSION[nProgress]" : "5";
    $n="20";
    echo ($n);
    exit();
}

1 个答案:

答案 0 :(得分:0)

我发现了问题。会话可以在连续调用网页之间存储值。我打算做的是在活动的PHP进程之间传递一个值。

执行此操作的一种方法是使用APC调用。然而,在5.3以上的php版本中不再可用,所以我选择了一种将进度信息存储在数据库中的方法。

它不是很有效,它使用了大量的资源。如果有人知道在活动的php进程之间共享变量的更好方法,那么在这里告诉它会很好。