我正在尝试使用Ajax发布请求中的数据更新HTML进度条。编号的值是从基于电子邮件成功的foreach循环中的PHP变量集中提取的。我正在努力寻找一种使用jQuery和Ajax将值从PHP发送到html进度条的方法。
我有一个标准的ajax请求,它运行良好,下面是我的PHP代码foreach循环:
foreach ($result as $person) {
$to = $person->post_title;
$headers = "From: " . bloginfo( 'name' ) ." <". get_option( 'admin_email' ) .">rn";
$headers .= "Reply-To: ". get_option( 'admin_email' ) . "rn";
$headers .= "MIME-Version: 1.0rn";
$headers .= "Content-Type: text/plain; charset="utf-8"rn";
$message = $content;
$send_email = wp_mail( $to, $subject, $message, $headers );
if( $send_email ) {
$email_count_update++;
}
// All emails have been sent
if( $email_count_update == $email_count ) {
$response->add( array(
'data' => 'success',
'supplemental' => array(
'message' => __( 'Emails sent', 'mup' )
)
));
$response->send();
}
}
任何帮助都会很棒,因为我整晚都没睡!
您可以尝试将其放入$_SESSION变量中,并使用js中的setInterval打印会话值,最后销毁会话变量
在电子邮件发送功能中添加设置$_SESSION值,该值将保存当前的$email_count_update值。为了避免用户调用多个请求时发生冲突,在AJAX请求中将JS中的唯一值发送到PHP。让它是$_GET['request_unique']。
在您的代码中添加:
if( $send_email ) {
$email_count_update++;
$SESSION['email_counter' . $_GET['request_unique']] = $email_count_update;
}
在PHP中创建单独的操作(让它响应url /counter
),用$_GET['request_unique']调用该操作将返回以下值:
$response->add(
array('email_count_update' => $SESSION['email_counter' . $_GET['request_unique']])
)->send();
在Javascript中,在请求发送操作后,每秒检查一次PHP会话值:
setInterval(function(){
//make AJAX request to /counter?request_unique=XXX
//get email_count_update from response and update progressbar
}, 1000);