1

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.