views:

55

answers:

2

In view page:

<script languge="javascript" type="text/javascript">
  $(document).ready(function() {
    $('#delete').click(function(e) {;
     e.preventDefault();
     var parent = $(this).parent();
     mainParent=parent.parent();

     $.ajax({ 
      type: 'post',
      url: "<?= site_url('controller_Test/fnDelete') ?>",
      data: "id="+$(this).prev().text(),

      success: function() {
       mainParent.slideUp(0,function() {
        mainParent.remove();
       });
      }

     });
     return false;
    });
});

Controller --controller_Test

function fnDelete(){
   $data['delete_me']=$_POST['id'];

    if (!empty($data['delete_me'])){
        $this->load->model('data_Model', '', TRUE);
        $this->properties->deleteRec($data['delete_me']);
        $this->output->set_output('works');
    } else {
        $this->output->set_output('dontwork');

    }
}

In this fnDelete should delete a row in the database. But its not deleting. but its removing that row from the view. Am using codeigniter. Is there anything wrong with my code.?

+2  A: 

First, you've got a strange semicolon:

$('#delete').click(function(e) {;

Remove that from the end. Then, try your data like this instead:

data: {"id":$(this).prev().text()}

You may also want to handle the response within your callback, and echo out the results so you know whether the PHP succeeded, or failed.

success: function(results) {
  if (results == "success") {
    /* remove associated elements */
  } else {
    alert("An error occurred.");
  }
}
Jonathan Sampson
I tried its not working still. I don't think that its executing the function mentioned in the url.
ASD
You need to handle the results in the success-function, so you know what is coming back.
Jonathan Sampson
Thanks a lot. It worked. I handled the results and found the problem with my query. Thanks again
ASD
Congrats! Great work.
Jonathan Sampson
A: 

In order to test you function you may want to use fiddler or you can also add an echo into your controller and an alert in you success function. For example:

  $(document).ready(function() {
    $('#delete').click(function(e) {;    
     e.preventDefault();
     $.ajax({ 
      dataType: 'text',
      success: function(responseText) {
       alert(responseText);
      }

     });
     return false;
    });
});

Controller --controller_Test

function fnDelete(){
   $data['delete_me']=$_POST['id'];
   echo 'deleting ' . $data['delete_me']
   // ...
}
bendewey