Php 下载CSV后显示成功消息
我开发了一个小的Wordpress插件,可以从数据库中下载用户的数据作为CSV文件 一切都很好,尽管我在弄清楚如何显示成功消息时遇到了一些问题 下面是负责数据导出的方法的代码:Php 下载CSV后显示成功消息,php,wordpress,Php,Wordpress,我开发了一个小的Wordpress插件,可以从数据库中下载用户的数据作为CSV文件 一切都很好,尽管我在弄清楚如何显示成功消息时遇到了一些问题 下面是负责数据导出的方法的代码: /* * Export CSV File */ public function export_CSV() { if ($this->users) { $fileName = "Export_users_" . date("Y-m-d_H:i:s", time()); $
/*
* Export CSV File
*/
public function export_CSV()
{
if ($this->users) {
$fileName = "Export_users_" . date("Y-m-d_H:i:s", time());
$fileName = $fileName.".csv";
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Description: File Transfer');
header('Content-Encoding: UTF-8');
header("Content-type: application/csv;charset=UTF-8");
header("Content-Disposition: attachment; filename={$fileName}");
header("Expires: 0");
header("Pragma: public");
echo "\xEF\xBB\xBF"; // UTF-8 BOM
$fh = @fopen( 'php://output', 'w' );
$headerDisplayed = false;
foreach ($this->users as $user) {
if($user) {
$user_meta = get_user_meta($user->data->ID);
//Push user meta into $user array
foreach($user_meta as $key => $var)
{
$user->data->$key = trim($var[0]);
}
//convert object in array;
$usersArray = (array)$user->data;
// Add a header row if it hasn't been added yet
if ( $headerDisplayed == false ) {
// Use the keys from $directory as the titles
fputcsv($fh, array_keys($usersArray));
$headerDisplayed = true;
}
// Put the data into the stream
fputcsv($fh,$usersArray);
}//end if
}//end foreach
// Close the file
fclose($fh);
// Users exported successfully
$this->response = "Users exported successfully!";
// Make sure nothing else is sent
exit;
} else {
// No results found
$this->response = "Sorry, no results found!";
}
}
我必须使用退出,否则CSV文件将包含页面的所有html。有没有办法运行javascript文件来更新前端的消息
提前谢谢 首先:
将下载详细信息放在单独的页面中。您只能发送一次标题,这就是为什么在发送csv标题后无法打印html的原因之一
其次,您可以为函数是否成功创建if/else语句。如果有,您可以添加一些JS
代码,如下例所示:
if($download = true) {
echo "<script> alert('Download succesfull!'); </script>";
}
else {
echo "<script> alert('Download failed!'); </script>";
}
if($download=true){
echo“警报('下载成功!');”;
}
否则{
echo“警报('下载失败!');”;
}
但我不确定它是否能像上面那样工作,因为这是一个服务器事件。另一种可能的解决方案是创建ServerEventCheck。首先:
将下载详细信息放在单独的页面中。您只能发送一次标题,这就是为什么在发送csv标题后无法打印html的原因之一
其次,您可以为函数是否成功创建if/else语句。如果有,您可以添加一些JS
代码,如下例所示:
if($download = true) {
echo "<script> alert('Download succesfull!'); </script>";
}
else {
echo "<script> alert('Download failed!'); </script>";
}
if($download=true){
echo“警报('下载成功!');”;
}
否则{
echo“警报('下载失败!');”;
}
但我不确定它是否能像上面那样工作,因为这是一个服务器事件。另一个可能的解决方案是创建一个ServerEventCheck。我提出了一个解决方案,将响应消息作为一个变量传递到报头中
$this->response = "Users downloaded Successfully!";
header("Location:?page=export-users&response=".$this->response);
然后像这样检索它:
<?php if(isset($_GET['response'])){ ?>
<div class="updated notice">
<p><?php echo __( $_GET['response']);?></p>
</div><!--error notice-->
<?php } ?>
我想出了一个解决方案,将响应消息作为变量传递到标题中
$this->response = "Users downloaded Successfully!";
header("Location:?page=export-users&response=".$this->response);
然后像这样检索它:
<?php if(isset($_GET['response'])){ ?>
<div class="updated notice">
<p><?php echo __( $_GET['response']);?></p>
</div><!--error notice-->
<?php } ?>
将下载内容放在单独的页面中。您只能发送一次标题,这就是为什么发送csv标题后无法打印html。据我所知,下载完成后无法运行某些内容。我想你可以使用ServerSentEvents或其他什么,但这似乎有点过头了。请将下载内容放在单独的页面中。您只能发送一次标题,这就是为什么发送csv标题后无法打印html。据我所知,下载完成后无法运行某些内容。我想你可以使用ServerSentEvents或其他什么,但这似乎有点过头了。这个解决方案不起作用。尽管如此,我还是想出了一个解决方案。无论如何谢谢@deathStorm这个解决方案不起作用。尽管如此,我还是想出了一个解决方案。无论如何,谢谢@Deathstorm