NN.一个小小的无刷新顶的功能废了我半天时间。一开始写好了。放在本地我电脑上。火狐,IE6,IE7测试通过。没想到让其它三个同事测试的时候缺全都报错。。。我人品有问题了??重新修改后进行
上个效果图先:
代码为三个文件:
第一个:ding.js
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=
/*
*--------------------------------------------------------------------
*
*创建一个XMLHttp实例
*return object 成功创建返回一个XMLHttp对象实例,否则返回false
*
*--------------------------------------------------------------------
*/
function CreateAjax()
{
var XMLHttp;
try
{
XMLHttp = new ActiveXObject("Microsoft.XMLHTTP"); //IE的创建方式
}
catch(e)
{
try
{
XMLHttp = new XMLHttpRequest(); //FF等浏览器的创建方式
}
catch(e)
{
XMLHttp = false; //创建失败,返回false
}
}
return XMLHttp; //返回XMLHttp实例
}
/*
*-------------------------------------------------------------------------
*
*函数名:Dig(id)
*功能:投票
*参数:id
* 1、id:文章的id
*
*-------------------------------------------------------------------------
*/
function usercheck(obj,cartype,type,ip)
{
_xmlhttp = CreateAjax();
var url = 'check.php?cartype='+cartype+'&type='+type+'&ip='+ip+'&n='+Math.random()+''; //这里添加了一个参数n,表示为一个随机数,以避免浏览器缓存
if(_xmlhttp) //判断XmlHttp是否创建成功
{
var content = document.getElementById(obj);
_xmlhttp.open('GET',url,true);
_xmlhttp.onreadystatechange=function()
{
if(_xmlhttp.readyState == 4) //客户端完成请求
{
if(_xmlhttp.status == 200) //服务端完成处理并返回数据
{
var ResponseText = unescape(_xmlhttp.responseText); //获取service的显示字符
//如果已经投票、service会显示Dig,投票数,这里分割字符用于显示错误和重置计数
if(ResponseText == "ding" ) //service返回了错误信息
{
alert("您今天已经投过票了!");
}else{
//服务器返回了成功,重置投票数
alert("投票成功!");
//将投一票改成查看文章的按钮
content.innerHTML=ResponseText;
}
}
else //服务器出现异常
{
alert("服务器返回异常!");
top.location.href='index.php';
}
}
}
_xmlhttp.send(null); //向服务器发送请求,因为是get请求,会直接附在URL后面,所以这里括号中的数据为null,IE中也可以不写,但FF就必须加上null,否则会发送失败。
}
else //创建未成功
{
alert("您的浏览器不支持或未启用 XMLHttp!");
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
第二个文件 check.php
<?php
$cartype=trim($_GET['cartype']);
$type=trim($_GET['type']);
$ip=trim($_GET['ip']);
$time=date("Y-m-d H:i:s");
$number=1;
mysql_connect(‘192.168.1.1','用户名','密码') or die ('连接数据库失败');
mysql_select_db('promise') or die ('选择数据库失败');
mysql_query('set names utf-8');
$result=mysql_query("insert into d_ding(number,type,cartype,ip,time) values($number,'".$type."','".$cartype."','".$ip."','".$time."')");
$res=mysql_query("select count(*) from d_ding where ip='".$ip."' and type='".$type."' and cartype='".$cartype."'");
);
echo $num;
?>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
第三个主文件:index.php
<?php
mysql_connect(‘192.168.1.1','用户名','密码') or die ('连接数据库失败');
mysql_select_db('promise') or die ('选择数据库失败');
mysql_query('set names utf-8');
$result=mysql_query("select count(*) from d_ding where type='wxca' and cartype='wxca'");
);
?>
<input type="submit" name="wx" id="ding1" value="" style="cursor:hand;" onClick="usercheck('num','wxca','wxca','<?php echo $ip; ?>')"></td>
<td rowspan="2"><strong>票数: <span id="num"><? echo $num; ?></span>票</strong></td>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++==
搞定~!!!
转载于:https://blog.51cto.com/ylj798/1063365