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.

4

1 回答 1

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

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

你忘记在那里初始化$db变量了吗?

尝试添加:

$db = Zend_Db_Table::getDefaultAdapter();

编辑

也试试这个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')
    });
});
于 2013-06-24T13:57:12.337 回答