javascript - how to insert values from toggle button to database without submit button -


i trying update values in database using toggle button here script

$(document).ready(function () {    $('input.status').on('change', function () {        var decision = $(this).val();        var id = $('td.myvalue').html();              alert(decision);         alert(id);         $.ajax({         type: "post",         url: "/codeigniter/users/users/update",         data: {decision: decision, id: id},         success: function (msg) {          $('#autosavenotify').text(msg);        }     })   }); }); 

datatable

<table class="table table-striped table-hover datatables "id="datatables" cellspacing="0" width="100%">     <thead>         <tr>             <th>icon</th>             <th>name</th>             <th>group users</th>             <th>status</th>              <th>action</th>         </tr>     </thead>     <tbody>         <?php $i = 0;             foreach ($gettablegroup $value) { ?>         <tr>             <td class="myvalue">                 <?= $value->id ?>             </td>             <td>                 <?= $value->group_name ?>             </td>             <td>                 <input class="tagsinput form-control" type="text" value="sam"/>             </td>             <input class="myid" type="hidden" value="<?= $value->id ?>"/>              <td >                 <?php if ($value->group_status == 'on'): ?>                 <div class="switch">                     <div class="onoffswitch">                         <input type="checkbox" checked class="onoffswitch-checkbox status" id="example<?= $i ?>" name="group-status" >                         <label class="onoffswitch-label" for="example<?= $i ?>">                             <span class="onoffswitch-inner"></span>                             <span class="onoffswitch-switch"></span>                         </label>                     </div>                 </div>                 <?php elseif ($value->group_status == 'off'): ?>                 <div class="switch">                     <div class="onoffswitch">                         <input type="checkbox" checked class="onoffswitch-checkbox status" id="example<?= $i ?>" name="group-status">                         <label class="onoffswitch-label" for="example<?= $i ?>">                             <span class="onoffswitch-inner"></span>                             <span class="onoffswitch-switch"></span>                         </label>                     </div>                 </div>                 <?php endif; ?>             </td>         </tr>         <?php             $i++;             }         ?>     </tbody> </table> 

my code working fine issue in scripting section when click on toggle button id not increase every time id came in alert box 1 in datatable show id number database

your fetching first td value $('td.myvalue') it's wrong need fetch changed tr td value

 var id = $('td.myvalue').html(); //it return first td .   

change

var id = $(this).parent('div').parent('div').parent('td').parent('tr').find('td.myvalue').html();   

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -