Welcome Guest, Not a member yet? Register   Sign In
Can't delete record via AJAX
#1

[eluser]mohssenvox[/eluser]
hi , sorry i'm not good in EN , but i can explain what i want..
i want to delete record using ajax here my view code:
Code:
[removed]
   jQuery(document).ready(function() {
    $(".deleteitem").click(function() {
     var pro = confirm("are you sure?");
     if (pro == true) {
      var parent = $(this).closest('TABLE');
      var id = parent.attr('id');
      var base_url = '<?php echo base_url();?>';
      $.ajax({
       type : "POST",
       data : "id=" + id,
       URL : base_url+"test/delMessage",
       success : function(msg) {
        $('#' + id).remove();
       }
      });
     }

    });

   });
[removed]
<?php
      $list = isset($messageInbox) ? $messageInbox : array();
      foreach ($list as $index) {
       echo "<table id=$index->id >";
       echo "<tr><td>from</td><td>$index->sender</td><td><button class=deleteitem ><div id=closeBTN title=delete  ></div></button></td></tr>";
       echo "<tr><td>subject</td><td>$index->subject</td><td></td></tr>";
       echo "<tr><td>body</td><td><div class='inbox_messageBody' >$index->body</div></td><td></td></tr>";
       echo "</table>";
      }
      ?&gt;

and here is my controller code:
Code:
public function delMessage() {
  $this -> load -> model("myDb");
  $data = $_POST["id"];  
  $this -> myDb -> deleteMessage($data);
}

My model:
Code:
public function deleteMessage($data) {
  $this -> db -> where("id", $data);
  $query = $this -> db -> delete("message");
  if ($query) {
   return TRUE;
  } else {
   return FALSE;
  }

}

i try to execute this code , but i cant , any idea..?
#2

[eluser]InsiteFX[/eluser]
Depending on the browser you are using hit F12 for the developer tools and see what your ajax is doing.

You may need to add index.php to your base_url
#3

[eluser]mohssenvox[/eluser]
tnx for reply. i use .htaccess file to remove index.php , i use it in url but i still have problem. i dont get any error , but this code dont delete row in my database only remove table from HTML
#4

[eluser]InsiteFX[/eluser]
In your ajax code the url should be lowercase not uppercase for one jQuery is case sensitive.

If you need index.php just change base_url to site_url

Code:
$.ajax({
       type : "POST",
       data : "id=" + id,
       URL : base_url+"test/delMessage",  // URL should be url
       success : function(msg) {
        $('#' + id).remove();
       }
      });

I do this in jQuery.
Code:
/* same as site_url() in CodeIgniter php
   CodeIgniter config.php - $config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-&?';
------------------------------------------------------------------------- */
function site_url(url) {
  return site_url + 'index.php?act=' + url;
}

// To use:
$.post(site_url('what_to_delete'), param, function(data) {
   // your  code
});

Good videos to watch:
Web Lee
#5

[eluser]mohssenvox[/eluser]
Tnx So Much...(Y) , it work find..
wish the best for you!




Theme © iAndrew 2016 - Forum software by © MyBB