I am looking to call a PHP function through an onclick event. The PHP action within the controller then should start a download for a json File containing the needed data.
I already know ajax and I am using it currently but somehow the function does not fire. I am 100% certain that I am doing something wrong, but I can’t seem to find what.
The ajax works great so far and the console.log()
at the end can be seen in the console. However, the function the request should trigger in my controller, does not go off. Either that or I am trying to log the wrong thing. Every bit of help is appreciated.
I am using Yii 1.1 and CButtonColumn.
admin.php:
'buttons'=>array( 'export' => array( 'label'=>'Export', 'click'=>'function() { $.ajax({ url: "/path/to/admin.php/followed/by/controller/action/actionExport", method: "POST", success: function(result) { console.log("Hello there!"); } }); }', 'imageUrl' => Yii::app()->request->baseUrl.'/images/image.png', ),
The adminController.php containing the action. Maybe I should mention that the controller is on another directory one level above the admin.php
public function actionExport(){ if (isset($_POST)) { Yii::log(It was a great success, I think!"); } $helper = new TestHelper(); $data = $helper->actionIndex(); header("Content-type: application/json/xml"); header("Content-Disposition: attachment; filename=report.json"); header("Pragma: no-cache"); header("Expires: 0"); echo json_encode($data); }
Advertisement
Answer
So thanks to some hints I was able to resolve the question and get it done. It was a problem with he URL and got to be done like so:
'url'=>'Yii::app()->createUrl(Yii::app()->controller->module->id."/".Yii::app()->controller->id."/export", array("id"=>$data["group_id"]))'