I am trying to submit a hidden form whenever a page is loaded, for which I have created a function in my controller. My problem is that I don't want to leave the current view, but I can not redirect to the view I want to stay in either because that would create a constant loop of loading the page, calling my function when loading has finished and then redirecting again. Therefore, I wanted to call this function without redirecting to the previous view but without rendering a new view either, for which I was using the following:
$this->autoRender = false;
$this->layout = false;
$this->render(false);
Nevertheless, these lines do not seem to be working for me, as the action keeps being redirected to this function trying to render a non existing view instead of staying in the previous one. Due to this, I'd like to know if there is another alternative for being able to call one function in my controller but staying at the page I am at (not by redirecting because of the looping aspect). In case it helps, I am adding the code I have for this action:
This is the part of the view I want to stay rendered where I have the functionality related to the hidden form
<?= $this->Form->create('Save data', array('url'=>'/exportations/save_data/'.$id, 'enctype' => 'multipart/form-data', 'method' => 'post', 'id' => 'data'))?>
<input type="hidden" id="svgGraph" />
<div class="" id="cont" style="display:none;"></div>
<?=$this->Form->end()?>
</div>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function(){
var chart = Highcharts.chart( // code to create graph
);
var svg = chart.getSVG();
$("#svg1").val(svg1);
});
$(document).ready(function () {
document.getElementById('data').submit();
});
</script>
This is my current controller '/exportations/save_data/' function:
public function save_data(){
$this->autoRender = false;
$this->layout = false;
$this->render(false);
if($this->request->is('post')) {
$data = $this->request->data;
$exportation = $this->Exportation->save($data);
}
return;
}
Currently, with this code, the data s succesfully stored, but the user ends up with an empty view, as the controller tries to render the save_data view.