我正在尝试使用ajax更新名为items的表中的数据库列(anert)。我已经从这个功能正常工作的网站的另一个地方复制了下面的代码。在那里,单击下拉列表中的值 - Inert(0)或Anert(1)成功更新数据库。但是,当我渲染当前页面时,即使db $ item ['anert']值当前设置为1(或Anert),下拉也会默认为Inert(或0)。另外,当我点击下拉列表的其他值时,它什么也没做。
我还应该注意到,在其他情况下,这是有效的,它循环通过一个数组,而在这里,我只是尝试更新一个值。
请帮忙。感谢。
<form action="<?php echo $this->form_action;?>" method="get">
<table cellspacing="0" cellpadding="0" class="display">
<?php if($this->item) { ?>
<tr id="<?php echo $item['id'];?>">
<td align="center" class="color-status-<?php echo $item['anert'];?>">
<?php if(in_array($item['anert'], array(0,1))) { ?>
<select class="event-item-status">
<?php if($item['anert']) { ?>
<option value="0"><?php echo $this->translate('Inert');?></option>
<option selected="selected" value="1"><?php echo $this->translate('Anert');?></option>
<?php } else { ?>
<option selected="selected" value="0"><?php echo $this->translate('Inert');?></option>
<option value="1"><?php echo $this->translate('Anert');?></option>
<?php } ?>
</select>
<?php } else { ?>
<?php echo $item['status'];?>
<?php } ?>
</td>
</tr>
<?php } else { ?>
<tr class="nodrop nodrag">
<td colspan="12">
<div class="msgAlert"><span><?php echo $this->translate('Attention!');?></span><?php echo $this->translate('No records found ...');?></div>
</td>
</tr>
<?php } ?>
</table>
</form>
<script type="text/javascript">
$(document).ready(function(){
$('.event-item-status').live('change', function() {
var status = this.value;
var id = $(this).parents('tr').attr('id');
$.ajax({
type: 'post',
url: "?module=items&controller=index&action=changeInertia",
data: 'id=' + id + '&status=' + status,
beforeSend: function () {
$('#'+id).animate({
'backgroundColor': '#FFBFBF'
}, 400);
},
success: function (result) {
if(result == 'ok') {
$.get(window.location.href, function(data){
$('#'+id).html($(data).find('#'+id).html());
setTimeout(function(){
$("#"+id+"").animate({'backgroundColor': 'transparent'}, 400).find('.tooltip').simpletooltip();
deletePage();
}, 500);
});
} else {
alert(result);
$("#"+id+"").animate({'backgroundColor': 'transparent'}, 400);
}
}
});
});
});
</script>
public function indexAction() {
if( !WM_Users::allow($this->getRequest()->getModule(),$this->getRequest()->getController(), $this->getRequest()->getAction()) ) {
$this->forward('admin', 'error', 'noPermission');
}
if($this->session->get('successfu_edite')) {
$this->view->successfu_edite = true;
$this->session->clear('successfu_edite');
}
if($this->session->get('error_permision')) {
$this->view->error_permision = $this->session->get('error_permision');
$this->session->clear('error_permision');
}
$ses = JO_Session::getAll();
$user = $this->getUser();
$cart = $this->getCart();
$request = $this->getRequest();
$item_id = $request->getParam('item_id');
$item = Items_Model_Item::find($item_id);
$this->view->form_action = WM_Router::create($request->getBaseUrl() . '?module=items&controller=index');
$item['status'] = Items_Model_Item::itemStatuses($item['anert']);
$this->view->item = $item;
}
public function changeInertiaAction() {
$this->noViewRenderer(true);
if( !WM_Users::allow($this->getRequest()->getModule(), $this->getRequest()->getController(), $this->getRequest()->getAction()) ) {
echo $this->translate('You do not have permission to this action');
} else {
$item = Items_Model_Item::find($this->getRequest()->getPost('id'));
if($item) {
$item->anert = $this->getRequest()->getPost('status');
if($item->save()) {
echo 'ok';
} else {
echo $item->getErrors();
}
} else {
echo 'error: item not found';
}
}
}
public static function itemStatuses($status = null)
{
$data = array(
0 => WM_Translate::t('Inert'),
1 => WM_Translate::t('Anert'),
2 => WM_Translate::t('Expired'),
3 => WM_Translate::t('Sold out')
);
if ($status !== null) {
return isset($data[$status]) ? $data[$status] : null;
}
return $data;
}