功能:

`点击图片,修改表里的状态值`

效果:

思路:

  1. 页面里在img里点绑定onclick件事,调用js函数listTable.toggle

    onclick="listTable.toggle(this, 'toggle_on_sale', {$goods.goods_id})"

  2. listTable.toggle函数里发送ajax.
    • 如果有错就报错
    • 如果没有错就把src里的图片地址给换了,

      obj.src = (res.content > 0) ? 'images/yes.gif' : 'images/no.gif';
  3. 控制器里接收到要修改的主键id和要修改的状态值0或者1,然后调用函数$exc->edit,修改表里的状态值,并返回json结果

页面里

<td align="center"><img src="images/{if $goods.is_on_sale}yes{else}no{/if}.gif" onclick="listTable.toggle(this, 'toggle_on_sale', {$goods.goods_id})" /></td>
<script type="text/javascript">
/**
* 切换状态
*/
listTable.toggle = function(obj, act, id) //act是要提交到的控制器名字
{
//val:要修改的表里的状态值为1或者为0
var val = (obj.src.match(/yes.gif/i)) ? 0 : 1; //i是不区分大小写
$.ajax({
type:"POST",
url:listTable.url,
data:"act="+act+"&val=" + val + "&id=" +id ,
dataType:"json",
async:false,
success:function(res){
if (res.message)
{
alert(res.message);
} if (res.error == 0)
{
//如果返回的值里有内容,就改它的src属性值
obj.src = (res.content > 0) ? 'images/yes.gif' : 'images/no.gif';
}
}
});
</script>

控制器里

实例化exchange类,把参数传过去
/**
* 构造函数
*
* @access public
* @param string $table 数据库表名
* @param dbobject $db aodb的对象
* @param string $id 数据表主键字段名
* @param string $name 数据表重要段名
*
* @return void
*/
$exc = new exchange($ecs->table('goods'), $db, 'goods_id', 'goods_name'); /*------------------------------------------------------ */
//-- 修改上架状态
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'toggle_on_sale')
{
check_authz_json('goods_manage'); $goods_id = intval($_POST['id']);
$on_sale = intval($_POST['val']); if ($exc->edit("is_on_sale = '$on_sale', last_update=" .gmtime(), $goods_id))
{
clear_cache_files(); //消除缓存
make_json_result($on_sale); //返回数据内容为状态值 (res.content=$on_sale)
}
} /**
* 清除缓存文件
*
* @access public
* @param mix $ext 模版文件名, 不包含后缀
* @return void
*/
function clear_cache_files($ext = '')
{
return clear_tpl_files(true, $ext);
} /**
*
*
* @access public
* @param
* @return void
*/
function make_json_result($content, $message='', $append=array())
{
make_json_response($content, 0, $message, $append);
} /**
* 创建一个JSON格式的数据
*
* @access public
* @param string $content
* @param integer $error
* @param string $message
* @param array $append
* @return void
*/
function make_json_response($content='', $error="0", $message='', $append=array())
{
include_once(ROOT_PATH . 'includes/cls_json.php'); $json = new JSON; $res = array('error' => $error, 'message' => $message, 'content' => $content); if (!empty($append))
{
foreach ($append AS $key => $val)
{
$res[$key] = $val;
}
} $val = $json->encode($res); exit($val);
}

D:\phpStudy\WWW\work\wdm\trunk\www.wdmcake.cn\www\admin\includes\cls_exchange.php

class exchange
{
var $table;
var $db;
var $id;
var $name;
var $error_msg; /**
* 构造函数
*
* @access public
* @param string $table 数据库表名
* @param dbobject $db aodb的对象
* @param string $id 数据表主键字段名
* @param string $name 数据表重要段名
*
* @return void
*/
function exchange($table, &$db , $id, $name)
{
$this->table = $table;
$this->db = &$db;
$this->id = $id;
$this->name = $name;
$this->error_msg = '';
} /**
* 编辑某个字段
*
* @access public
* @param string $set 要更新集合如" col = '$name', value = '$value'"
* @param int $id 要更新的记录编号
*
* @return bool 成功或失败
*/
function edit($set, $id)
{
$sql = 'UPDATE ' . $this->table . ' SET ' . $set . " WHERE $this->id = '$id'"; if ($this->db->query($sql))
{
return true;
}
else
{
return false;
}
}

最新文章

  1. C#输出文本树形层次,前或者后自定义空格位数
  2. Node Express 4.0 安装
  3. Scala的sealed关键字
  4. C++矢量图形库系列(1)——矢量图形库乱谈(转)
  5. string.join加引号
  6. publish_subscribe
  7. python的相对路径导入问题
  8. 一起啃PRML - Preface 前言
  9. powerdesigner设置mysql唯一键,非主键
  10. kafka的一些常用命令
  11. Oracle中sys和system用户的区别
  12. mybatis系列笔记(2)---mapper代理方法
  13. 前端魔法堂:屏蔽Backspace导致页面回退
  14. [leetcode-629-K Inverse Pairs Array]
  15. Linux - 简明Shell编程05 - 条件语句(Case)
  16. 1_NAT模式和桥接模式下的网络配置
  17. 使HTML5支持RTSP流 微信直播RTSP流 微信播放RTSP直播流(HTML5播放rtsp,web播放rtsp,微信支持rtsp)
  18. mysql 表
  19. flower
  20. jmeter压力测试及抓包

热门文章

  1. Gym 101047M Removing coins in Kem Kadr&#227;n
  2. vs2017通过snippet代码片断进行标准化注释
  3. NewStar 信息分发系统设计
  4. python协程与异步协程
  5. Java8 如何正确使用 Optional
  6. Fiddler 抓包工具总结(转)
  7. ArrayList与Vector区别
  8. 【Java/Android性能优 6】Android 图片SD卡缓存 使用简单 支持预取 支持多种缓存算法 支持不同网络类型 支持序列化
  9. hive 报错FAILED: Error in metadata: java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.metastore.HiveMetaStoreClient FAILED: Execu
  10. pta数据结构编程题