jquery - AJAX + Codeigniter live page load with function -


updated: works 100%.

these segements of code allowed user click delete button on view, sends ajax request controller interact (delete record) database. feed result of occured in callback function update current view (without page load) record deleted , hide element.

view:

<?php // output records if($result) {     foreach ($result $rows => $row)      {         echo "<tr id=\"row" . $row['id'] . "\">";             echo "<td>" . $row['id'] . " - " . $row['name'] . "</td>";             echo "<td>" . $row['logo_path'] . "</td>";             echo "<td>" . $row['date_created'] . "</td>";             echo "<td>" . "<a href=\"#\" id=\"" . $row['id'] . "\" class=\"delete\">delete</a><br>" . "</td>";         echo "</tr>";     }  } else {     echo "there no platforms show"; } ?>  <script type="text/javascript"> $("a.delete").click(function(e)  {     e.preventdefault();     var platform_id = $(this).attr('data-id');     var row = $(this).attr('id');      $.ajax({         type: "post",         url: "platform/delete",         datatype: "json",         data: 'platform_id='+platform_id,         success: function(result){             if (result.success == 1)             {                 $("#row" + row).fadeout();                 //document.getelementbyid(row).style.display = 'none'             }         },         error: function(result){             alert(result.message);         }     }); }); </script> 

controller:

function delete() {     $result=array();       $this->load->model('platform_model');     $platform_id = $this->input->post('platform_id');      if($this->platform_model->delete($platform_id))     {         $result['success']= 1;         $result['platform_id']= $platform_id;         $result['message']= "success";     } else {         $result['success']= 0;         $result['message']= "error";     }      die(json_encode($result));  } 

model:

function delete($platform_id) {     $this->db->where('id =', $platform_id);      if ($this->db->delete('platforms'))     {         return true;     }     else     {         return false;     }    } 

update

you don't need form code make work. in updated question there missing define id variable. without not able delete record.

data sent http post request unregarded if form submits it. ajax code data sent post data also, handled codeigniter $this->input->post() array in ajax model method. great tool debug process firebug wher can see data posted , returned (net/xhr tab)

in example before used attribute data-id instead of id attribute latter need unique. prefer solution myself use attribute name except id (if attribute value repeated).

$('a.delete').click(function(e)  {    e.preventdefault();    var id = $(this).attr('data-id');    // ajax call 

matching html anchor button or other element:

<a href="#" data-id="<?php echo $row['id']; ?>" class="delete">delete</a> 

the ajax code triggered clicking of delete link, hence no need form.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

qt - Errors in generated MOC files for QT5 from cmake -