1

I'm trying to get Modal by clicking on a button to call my modal with form to add users :

html code:

<button data-toggle="modal" data-target="#mymodal" data-id="<?php echo 'manager_user/user_add';?>" id="menu" class="btn btn-sm btn-primary">
Add User</button>

and in my Modal form the input with csrf name & hash is inserted !

<input type="hidden" name="csrf_token" value="<?=$this->security->get_csrf_hash();?>">

ajax #menu is called by the button above.

    $(document).ready(function() {
        $(document).on('click', '#menu', function(e) {
            e.preventDefault();
            var url = $(this).data('id'); // it will get action url
            $('#dynamic-content').html(''); // leave it blank before ajax call
            $('#modal-loader').show(); // load ajax loader

            $.ajax({
                url: url,
                type: 'POST',
                dataType: 'html'
            })
            .done(function(data) {
                console.log(data);
                $('#dynamic-content').html('');
                $('#dynamic-content').html(data); // load response 
                $('#modal-loader').hide(); // hide ajax loader 
            })
            .fail(function() {
                $('#dynamic-content').html('<i class="glyphicon glyphicon-info-sign"></i> Something went wrong, Please try again...');
                $('#modal-loader').hide();
            });
        });
    });



$config['csrf_protection'] = TRUE;
$config['csrf_token_name'] = 'csrf_token';
$config['csrf_cookie_name'] = 'csrf_cookie_token';
$config['csrf_expire'   ] = 7200;
$config['csrf_regenerate'] = FALSE;

When i turn csrf_protection to FALSE it works

I did also add a meta to my header !! so when I click to show my modal to add user I get 403 Forbidden ERROR.

and Error AJAX Message : Something went wrong, Please try again...

how to I resolve the csrf issue?

Vickel
  • 7,751
  • 6
  • 34
  • 54
abdelghani
  • 21
  • 2
  • also this answer expains pretty well how to implement csrf with ajax: https://stackoverflow.com/a/16140018/2275490. the trick is to send the csrf token with your ajax call to the controller, get it approved and create+send a new token back to the html with the ajax done/success function. This repeats with each ajax call – Vickel Apr 06 '20 at 22:48
  • Good ! i will try it and look the link you share ! thanks – abdelghani Apr 07 '20 at 00:36

0 Answers0