视图层
<td>
<?php if($value['is_on_sale'] == 1) {?>
<img src="../web/images/yes.gif" onclick="but(<?php echo $value['goods_id']?>,this)">
<?php }else { ?>
<img src="../web/images/no.gif" onclick="but(<?php echo $value['goods_id']?>,this)">
<?php } ?>
</td>
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
js代码
<script src="../web/js/jquery-1.4.4.min.js"></script>
<script type="text/javascript">
function but(id,obj){
var src = $(obj).attr('src');
var ajax = new XMLHttpRequest();
ajax.open('get','index.php?r=third/upsale&id='+id,true);
ajax.send();
ajax.onreadystatechange=function(){
if(ajax.readyState==4){
// location.href='javascript:history.go(0)';
if(ajax.responseText==1){
if(src=='../web/images/no.gif'){
$(obj).attr('src','../web/images/yes.gif')
}else{
$(obj).attr('src','../web/images/no.gif')
}
}
}
}
}
</script>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
控制器
public function actionUpsale(){
$goods_id = $_GET['id'];
$models = new EcsGoods();
$result = $models -> changestate($goods_id);
return $result;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
model层
public function changestate($goods_id=''){
$arr = $this->findBySql("SELECT 上下架字段 FROM 表名 WHERE goods_id=$goods_id")->asArray()->one();
if($arr['上下架字段']==0){
$sql = "UPDATE 表名 set 上下架字段 = '1' WHERE goods_id=$goods_id";
$return=Yii::$app->db->createCommand($sql)->execute();
echo $return;die;
}else{
$sql = "UPDATE 表名 set 上下架字段= '0' WHERE goods_id=$goods_id";
$return=Yii::$app->db->createCommand($sql)->execute();
echo $return;die;
}
}
效果如下