javascript - Ajax request does not update database until page refresh -


i'm making todo list dashboard system i'm creating. view todo list

so idea user can add new todo items, delete todo items , finish/unfinish todo items. i'm working in project codeigniter.

the problem when i'm adding task added database when delete after or finish it, has not been updated in database. when refresh page , delete task or finish task after updated in database. have checked data , has been succesfuly sended controller , controller model. not updated in database reason.

i'm sending data using ajax post requests codeigniter controller.

$(document).ready(function () { runbind(); function runbind() {      /**      * deletes task in list      */     $('.destroy').on('click', function (e) {         var $currentlistitem = $(this).closest('li');         var $currentlistitemlabel = $currentlistitem.find('label');         $('#main-content').trigger('heightchange');         $.ajax({             url: 'dashboard/deletetodo',             type: 'post',             data: 'message=' + $currentlistitemlabel.text()         }).success(function (data) {             $currentlistitem.remove();         }).error(function () {             alert("error deleting item. item not deleted, please try again.");         })     });      /**      * finish task or unfinish depending on data attribute.      */     $('.toggle').on('ifclicked', function (e) {         console.log("hallo");         var $currentlistitemlabel = $(this).closest('li').find('label');         /*          * or add css , remove js dynamic css.          */         if ($currentlistitemlabel.attr('data') == 'done') {             $.ajax({                 url: 'dashboard/finishtodo',                 type: 'post',                 data: 'message=' + $currentlistitemlabel.text() + '&finish=' + false             }).success(function (data) {                 console.log(data);                 $currentlistitemlabel.attr('data', '');                 $currentlistitemlabel.css('text-decoration', 'none');             }).error(function () {                 alert("error updating item. item not updated, please try again.");             })         }         else {             $.ajax({                 url: 'dashboard/finishtodo',                 type: 'post',                 data: 'message=' + $currentlistitemlabel.text() + '&finish=' + true             }).success(function (data) {                 console.log(data);                 $currentlistitemlabel.attr('data', 'done');                 $currentlistitemlabel.css('text-decoration', 'line-through');             }).error(function () {                 alert("error updating item. item not updated, please try again.");             })         }     }); }  $todolist = $('#todo-list');  /**  * add new task.  */ $("#frm_todo").submit(function (e) {     e.preventdefault();     var url = $(this).attr('action');      var method = $(this).attr('method');     var data = $(this).serialize();     $.ajax({         url: url,         type: method,         data: data     }).success(function (data) {         additemtohtmllist();         $('#new-todo').val('');     }).error(function () {         alert("error saving task. task has not been saved, please try again.");     }); });  /**  * adds task has been created directly html page  */ var additemtohtmllist = function () {     $('.destroy').off('click');     $('.toggle').off('click');     var todos = "";     todos +=         "<li>" +         "<div class='view'>" +             "<div class='row'>" +                 "<div class='col-xs-1'>" +                     "<input class='toggle' type='checkbox'>" +                 "</div>" +                 "<div class='col-xs-10'>" +                     "<label id='item'>" + " " + $('#new-todo').val() + "</label>" +                 "</div>" +                 "<div class='col-xs-1'>" +                     "<a class='destroy'></a>" +                 "</div>" +             "</div>" +         "</div>" +         "</li>" + $todolist.html();     $todolist.html(todos);     $todolist.find('input').icheck({checkboxclass: 'icheckbox_flat-grey', radioclass: 'iradio_flat-grey'});     runbind();     $('#main').show(); } }); 

it great if can me out because have no clue. have checked data not updated in database before page refresh.

have compared xhr requests using browser dev tools ensure 100% equivalent (for delete on existing todo versus delete on newly added todo)?

also, typical pattern use sort of id instead of using whole message matcher -- have considered having finish/delete operate on id of todo instead of sending text of todo? can make unique id if not want expose database ids.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -