How to use csrf class in codeigniter with onchange event?

Asked

Viewed 142 times

0

I have the following situation:

Form

<?php
    $atributos = array('id'=>'cadloja','class'=>'form-inline','role'=>'form','data-toggle'=>'validator');
    echo form_open('',$atributos);
?>
    <select class="form-control" id="grupo">
        <option value="<?= 'retorno_ajax' ?>"><?= 'retorno_ajax' ?></option>
    </select>

<?php
    echo form_close();
?>

Javascript

$('#grupo').change(function () {
    $('#div_retorno').load('lista_grupos');
}

Note that I am not using ajax requests or submitting the form in the event onchange. With the csrf active, the onchange doesn’t work anymore.

I even found some answers on SOen, but at all, would be forced to make another ajax request at the event onchange. I read in the documentation of CI also that it is possible to exclude a page from the verification csrf:

 $config['csrf_exclude_uris'] = array('controllerx/metodox');

That option is the least laborious, but would be safe yet?

So, is this... is there any way to do it differently than the two I was talking about? Thank you.

  • Of course you’re using requisitions. load() is a request. And as the csrf is interfering, it’s being done via _$POST. Show the whole code.

1 answer

0


Sorry for the delay, it’s true, after several searches, I found the answer. To "standardize" the work in any case, I saw that using ajax is easier, and also, da para usar o when and then of jquery together.

The point is that the CI only automatically refreshes the token if the whole page is reloaded, as this is not the case, you must return the new token from the controller for the view.

If you simply try to use echo $this->security->get_csrf_hash(); directly in the view, it does not work. It has to come from controller or even from model.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.