Im trying to generate a basic csv file with some data. When I use an alert inside the ajax call it will show me the data(so there is data passing), but when I click the button it will not generate an CSV file. Im new to yii2 so im still learning.
UPDATED I have changed the files
//view export/index.php
Pjax::begin();
$form = ActiveForm::begin([
'action' => yii\helpers\Url::to(['cms-export/index']),
'options' => ['data' => ['pjax' => true]],
'layout' => 'horizontal',
'fieldConfig' => [
'horizontalCssClasses' => [
'label' => 'col-sm-2',
'offset' => 'col-sm-offset-2',
'wrapper' => 'col-sm-5',
'hint' => 'col-sm-5',
],
],
]);
echo $form->field($model, 'language')->dropDownList([//some list]);
echo $form->field($model, 'filename')->textInput()
echo Html::submitButton('Submit', ['class' => 'btn btn-primary'])';
ActiveForm::end();
Pjax::end();
//model
public function generateCsv(){
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename="sample.csv"');
$data = [datacomeshere];
$fp = fopen('php://output', 'w');
foreach ( $data as $line ) {
fputcsv($fp, $line, ';');
}
fclose($fp);
}
//controller
public function actionIndex()
{
$model = new Export();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
// validation works, but method does not work
\common\models\Export::generateCsv();
}
return $this->render('index' , ['model' => $model]);
}
When I click the button it will show me an 500 error in the jquery file
xhr.send( options.hasContent && options.data || null );