0
votes

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.

1
What should the user see instead of a blank page?Greg Schmidt
The user should see the view from which the hidden form is called, which consists of a page with some loaded texts and another form from which the texts can be editted. At first, I was calling this view's method to save the graph too, but as when saving those texts the page is redirected to the same page but reloading the texts, that would cause the infinite loop I was previously mentioning. I hope I have managed to explain myself.pepito
When your form is posted, the browser is going to go from the page that shows the form to the page that results from processing the form. That's how forms work, by default. If you want to keep the original page there, then you need to post your form via Ajax.Greg Schmidt

1 Answers

0
votes

If there is only 1 (or a few) actions that load this form, you can handle this within the action.

In controller action that renders form initially, check for post and data, do what you need to, then complete the rest of the action and finish rendering the view. You don't have to redirect on post.

This way on save success you can set a request data param, which can be reflected in the view, and if set, the JS doesn't "re-fire" the form.

The AJAX idea from @Greg Schmidt in terms of UX is a better solution, I would choose that over my suggestion any day. I only propose this if you don't want to use AJAX