MohamedB MohamedB - 7 months ago 25
Javascript Question

Using Ajax_Jquery to perform a delete action in Zend

I'm new to Ajax/jQuery.

I want to delete a row (email) in the database (emails table) when user presses a link in my view.

Here is my view example:

<a href="delete/id/<?= $email['id'] ?>" class="btn">
<i class="icon-trash icon-white"></i>
Delete
</a>


I have an action in my controller
deleteAction()
that deletes entries, I want to call this action with some jQuery so that I don't need to go to delete view page.

Answer

create a delete action in your zend controller

  <?php
public function deleteAction() {

$this->_helper->layout()->disableLayout(); 
$this->_helper->viewRenderer->setNoRender(true);
$request = $this->getRequest();
$id= $request->getPost('id');
/*
your code to delete the row from database using this id
when row deleted successfully then echo 'success' else echo 'failed'
*/
}
?>

now come to jquery ajax code

<script type="text/javascript">
function deleteRow(id) {
        var baseurl = '<?php echo baseUrl();?>';
        $.ajax({
        url: baseurl+"/delete/id/"+id,
        type:'GET',
        success:function(res){
        if(res== 'success'){
          alert("row deleted successfully");
       } else {
         alert("failed to delete row");
       }
       }
      });
}
    </script>

call the deleteRow function on your delete button and pass the id in it.

Comments