0

I've searched SO for an answer to this, and even though I can find answers relating to the Zend framework and AJAX, I am not able to fix my problem.

I want to do delete a table row via an AJAX POST method.

My reason for doing this is that my web developer can longer make changes to my project, so I am having to step in and attempt to do some things myself (so I'll overlook somewhat obvious things).

HTML:

<a class="btn btn-danger delete_request_button" data-rid="<?php echo $row['id']?>" data-did="<?php echo $row['dealer_id']?>" href="#delete_request_button" data-toggle="modal">Delete</a>

jQuery:

jQuery('.delete_request_button').click(function(){
    var id = jQuery(this).attr('data-rid');
    jQuery('#request_id').val(id);

    jQuery.post('<?php echo $this->baseUrl("requests/delete")?>', { id: id }, function(response) {

    }).complete(function(){
        alert(id + ' deleted')
    });
});

Controller:

function deleteAction() 
{
    $id = $this->getRequest()->getParam('id');
    if($request->isPost()) {
        $this->model_dealer->deleteRequest($id);
        exit();
    }
}

Model:

public function deleteRequest($id)
{
    $where = $db->quoteInto('id = ?', $id);

    $db->delete('dealers_vehicle_requests', $where);
}

And here is my table:

CREATE TABLE `dealers_vehicle_requests` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `dealer_id` bigint(20) DEFAULT NULL,
  `message` text,
  `requested_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `status` char(1) DEFAULT 'P',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;

I realise that this is probably a mashup of horrible code, but I'm using SO as a last resort to try and solve this problem.

Thanks!


Edit: fixed

Model:

public function deleteRequest($id)
{
    $where = $this->dealers_subscriptions->getAdapter()->quoteInto('id = ?', $id);
    $this->dealers_vehicle_requests->update(array('status' => 'D'), $where);
}

Controller:

function deleteAction() 
{
    $request = $this->_request;
    $id = $request->getParam('id');

    if($request->isPost()) {
        $this->model_dealer->deleteRequest($id);
        exit();
    }
}

Everything else kept the same.

Thanks to all that helped with this question.

1
  • 1
    Nothing is actually being deleted from the database. I get an alert, for example "9 deleted", but when I refresh the row is still there and the database is unaffected. Commented Jun 24, 2013 at 14:21

1 Answer 1

1
public function deleteRequest($id)
{
    $where = $db->quoteInto('id = ?', $id);

    $db->delete('dealers_vehicle_requests', $where);
}

Did you forget to initialize $db variable there ?

Try to add:

$db = Zend_Db_Table::getDefaultAdapter();

EDIT

Also try this JS :

jQuery('.delete_request_button').click(function(){
    var id = jQuery(this).attr('data-rid');
    jQuery('#request_id').val(id);

    jQuery.post('<?php echo $this->baseUrl("requests/delete")?>', { id: id }, function(response) {
        alert(id + ' deleted')
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your reply. I added that line, but it still doesn't seem to be working. For what it's worth, I do get an alert upon pressing the button (with the correct id too), it just doesn't seem to be communicating with the server. Edit: In a model created by my developer, he uses this syntax: $this->dealers_vehicle_requests->insert(array('dealer_id' => $id, 'message' => $message));

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.