【问题标题】:Progressbar using php and jquery使用 php 和 jquery 的进度条
【发布时间】:2016-11-01 16:44:52
【问题描述】:

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

程序循环访问从数据库派生的邮件地址数组。首先验证邮箱地址是否存在于邮箱中。如果没有,就会被举报。收集报告并最终发送回浏览器。

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

【问题讨论】:

    标签: php jquery ajax progress-bar


    【解决方案1】:

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

    一种方法是使用 APC 调用。但是,这在 5.3 之后的 php 版本中不再可用,因此我选择了一种将进度信息存储在数据库中的方法。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-29
      • 2013-09-05
      • 2012-05-19
      • 1970-01-01
      • 1970-01-01
      • 2011-12-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多