php - AJAX request from Codeigniter view gives an error -


csrf protection enabled.

i have view enter image description here

i trying insert shifts database table via ajax.

$('#insert_shift').click(function(e) {     e.preventdefault();     var empty_td = $('.td_shift[data-type=""]').size();     if (empty_td == 0) {         var date = $('#atdnc_date').val() + '-';         var arr = [];         var new_arr = [];         $('.td_shift').each(function() {              //if($(this).attr('data-day') != 0){              var data_type = $(this).attr('data-type');             var shift_atdnc_id = $(this).attr('data-typeid');             var user_id = $(this).attr('data-user');             var new_date = date + $(this).attr('data-day');              if (data_type == 'shift') {                 var shift_strt_time = $(this).attr('data-start');                 var shift_end_time = $(this).attr('data-end'); // change new_arr old                 var new_arr = {                     'shift': shift_atdnc_id,                     'user_id': user_id,                     'date': new_date,                     'shift_strt_time': shift_strt_time,                     'shift_end_time': shift_end_time,                     'checkin_time': '00:00:00',                     'checkout_time': '00:00:00',                     'time_spent': '00:00:00',                     'checkin_reason': 'na',                     'checkout_reason': 'na',                     'work_report': 'na',                     'attn_status': 0                 };             } else if (data_type == 'attendance') {                 var new_arr = {                     'shift': shift_atdnc_id,                     'user_id': user_id,                     'date': new_date,                     'shift_strt_time': '00:00:00',                     'shift_end_time': '00:00:00',                     'checkin_time': '00:00:00',                     'checkout_time': '00:00:00',                     'time_spent': '00:00:00',                     'checkin_reason': 'na',                     'checkout_reason': 'na',                     'work_report': 'na',                     'attn_status': shift_atdnc_id                 };             }              arr.push(new_arr);              //}          });         $.post(base_url + 'test_shift/insert_shift', {                 a: arr,                 csrf_test_name: csrf_token             },             function(data) {                 alert(data);                 if (data == 1) {                     document.location.href = base_url + 'test_shift';                 } else {                     alert("error");                 }             }         );     } else {         alert("please fill shifts");     } }); 

if there 2 rows of shift, values getting inserted. if there more, 3 in case, nothing getting inserted db getting error in console.

an error encountered action have requested not allowed.

when did search on error came know thrown in case of csrf issues. couldn't find way fix issue. please help?

update

when changed order of data in post above error has disappeared. 1 came up.

a php error encountered

severity: notice message: array string conversion

filename: mysql/mysql_driver.php line number: 589

and database error. insert query goes

insert table_name () values ('1','some_value'),('2','some_value2'),array 

the code used generate array of items same arr[83] problem starts. have tried deleting tds after 83rd day(which day 23 in 3rd row) browser , code worked. don't understand happening here. 83rd array got chopped reason php(or ci).

the issue fixed. elements(starting array(83)) of array being passed controller ajax post getting popped, lead database error. don't know exact reason yet after research found this thread. instead of inserting values in single insert query have inserted 1 employee's 1 month's shift in 1 insert query, safe.

modified javascript this

$('#insert_shift').click(function(e) {    e.preventdefault();      var empty_td = $('.td_shift[data-type=""]').size();      if (empty_td == 0) {        var num_of_days = parseint($('.th_day').last().text());      var num_of_users = parseint($('.user_shift_row').size());      var insert_helper = 0;      var post_finish_helper = 0;        var date = $('#atdnc_date').val() + '-';      var arr = [];      var new_arr = [];        function insert_shift_per_user() {            $('.td_shift[data-inserted="0"]').each(function() {              insert_helper++;              var data_type = $(this).attr('data-type');            var shift_atdnc_id = $(this).attr('data-typeid');            var user_id = $(this).attr('data-user');            var new_date = date + $(this).attr('data-day');              if (data_type == 'shift') {              var shift_strt_time = $(this).attr('data-start');              var shift_end_time = $(this).attr('data-end'); // change new_arr old              var attn_status = 0;              var shift = shift_atdnc_id;            } else if (data_type == 'attendance') {              var shift_strt_time = "00:00:00";              var shift_end_time = "00:00:00";              var attn_status = shift_atdnc_id;              var shift = 0;            }              new_arr = {              'user_id': user_id,              'date': new_date,              'shift': shift,              'attn_status': attn_status,              'shift_strt_time': shift_strt_time,              'shift_end_time': shift_end_time            };              arr.push(new_arr);              if (insert_helper == num_of_days) {                $.post(base_url + 'test_shift/insert_shift', {                    csrf_test_name: csrf_token,                  a: arr                },                function(data) {                    if (data == 1) {                      arr = []; // clearing array                    new_arr = []; // clearing new_array                    insert_helper = 0;                      $('.td_shift[data-user="' + user_id + '"]').attr('data-inserted', 1);                      post_finish_helper++;                      if (post_finish_helper == num_of_users) {                      document.location.href = base_url + 'test_shift';                    } else {                      insert_shift_per_user();                    }                  } else {                    alert("error");                  }                  }              );                  } //if insert_helper == num_of_days ends            });          } // function insert_shift_per_user        insert_shift_per_user();          //var aa = jquery.parsejson(arr);      //console.log(arr);      } else {      alert("please fill shifts");    }        }); //insert_shift ends here


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -