我正在尝试从wordpress定制表中导出数据。我有两个jquery UI datepicker,分别代表开始和结束日期。我通过单击按钮通过wp ajax发送日期归档值。如果我传递的日期值是静态的,则csv会生成,但是将值存储在变量$ sdate和$ edate中将不起作用。如果我通过将静态值存储在那些也可以工作的变量中。我可以看到ajax函数正在提交日期字段值。不确定代码有什么问题。
这是我的功能:
{
"/mag": {
"target": {
"host": "mag.mywebsite.com",
"protocol": "http:",
"port": 80
},
"secure": false,
"changeOrigin": true,
"logLevel": "info",
"pathRewrite": {"^/mag" : ""}
}
}
我的jquery:
add_action( 'wp_ajax_sharepricedownload', 'sharepricedownload' );
add_action( 'wp_ajax_nopriv_sharepricedownload', 'sharepricedownload' );
function sharepricedownload() {
global $wpdb;
$sdate=$_REQUEST['sdate'];
$edate=$_REQUEST['edate'];
// $sdate='2018-06-19';
// $edate='2018-06-19';
$file = 'shareprice';
$results = $wpdb->get_results("SELECT TICKER,DATE,OPEN FROM share_prices WHERE DATE BETWEEN '$sdate' AND '$edate' ", 'ARRAY_A');
//$results = $wpdb->get_results("SELECT TICKER,DATE,OPEN FROM share_prices WHERE DATE BETWEEN '2018-06-19' AND '2018-06-19' ", 'ARRAY_A');
if (empty($results)) {
return;
}
$head=array('TICKER','DATE','OPEN');
$fp= fopen('php://output', 'w');
fputcsv($fp,$head);
//fputcsv($fp,$results);
foreach ($results as $row) {
fputcsv($fp, $row);
//echo $row;
}
$csv_output .= "\n";
$filename = $file;
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit();
}