ajax同步刷新

时间:2023-03-09 09:48:37
ajax同步刷新

\js\ajax.js

//图片形式状态切换
function toggle(url,action,obj,str,id){//状态转换/obj-当前对象/str-传入类型字符串/id-传入ID
$.ajax({
type: "post",
url: url,
cache: false,
data: "id="+id+"&action="+action+"&str="+str+"&t="+tmp.toString(38),
success: function(data){
if(data=="0"){
obj.src = "/images/yes.png";
}else if(data=="1"){
obj.src = "/images/no.png";
}else{
alert("参数出错");
}
}
});
}

前台调用代码

index.php

<?
$nns1 = $row[mstatus]== 0?"/images/yes.png":"/images/no.png";
?>
<img src="<?=$nns1?>" style="cursor:pointer;" onclick="toggle('operate_do.php','ajStatus',this,1,'<?=$row[id]?>')" />

数据库操作代码

operate_do.php

<?require('includes/global.php');?>
<?
$action = $_REQUEST["action"]; switch($action){
case "ajStatus":
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); $id=$_REQUEST["id"];
$str=$_REQUEST["str"]; switch ($str){
case 1:
$table ="map";
break;
case 2:
$table ="map2";
break;
} $cs=mysql_query("select mstatus from ".$table." where id=".$id."",$dbh);
if(mysql_num_rows($cs)>0){
$row=mysql_fetch_array($cs);
$mstatus=$row[mstatus];
if($mstatus == 1){
mysql_query("update ".$table." set mstatus=0 where id=".$id."",$dbh);
echo "0";
}else if($mstatus == 0){
mysql_query("update ".$table." set mstatus=1 where id=".$id."",$dbh);
echo "1";
}
}
break;
}
?>