我是正做的一个仿美丽说的 jQuery+Ajax+PHP实现“喜欢”评级 功能,现在已经做到了点击图片“喜欢”的数量发生改变。
当点击彩色图片时,图片变成另一张灰色的,数量显示为增1。
当点击灰色图片时,数量减1,同时图片变成彩色的。
现在已经实现了大部分功能,只有图片的切换没有实现,希望有高手能帮助解决这个问题。
现贴上代码。
index.php页代码:
jQuery+Ajax+PHP实现“喜欢”评级 <script></script>
<script> <br /> $(function(){ <br /> $("div a").click(function(){ <br /> var love = $(this); <br /> var id = love.attr("rel"); <br /> love.fadeOut(300); <br /> $.ajax({ <br /> type:"POST", <br /> url:"love.php", <br /> data:"id="+id, <br /> cache:false, <br /> success:function(data){ <br /> love.html(data); <br /> love.fadeIn(300); <br /> } <br /> }); <br /> return false; <br /> }); <br /> }); <br /> </script>
include_once("connect.php");
$sql = mysql_query("select * from pic");
while($row=mysql_fetch_array($sql)){
$pic_id = $row['id'];
$pic_name = $row['pic_name'];
$pic_url = $row['pic_url'];
$love = $row['love'];
$qqqwww = 2;
?>
love.php页面代码:
include_once("connect.php");
$ip = get_client_ip();
$id = $_POST['id'];
if(!isset($id) || empty($id)) exit;
$ip_sql=mysql_query("select ip from pic_ip where pic_id='$id' and ip='$ip'");
$count=mysql_num_rows($ip_sql);
if($count==0){
$sql = "update pic set love=love+1 where id='$id'";
mysql_query( $sql);
$sql_in = "insert into pic_ip (pic_id,ip) values ('$id','$ip')";
mysql_query( $sql_in);
$result = mysql_query("select love from pic where id='$id'");
$row = mysql_fetch_array($result);
$love = $row['love'];
echo $love;
}else{
//echo "喜欢过了";
$sql = "update pic set love=love-1 where id='$id'";
mysql_query( $sql);
//$sql_in = "insert into pic_ip (pic_id,ip) values ('$id','$ip')";
//mysql_query( $sql_in);
$sql_in = "delete from pic_ip where ip='$ip'";
mysql_query($sql_in);
$result = mysql_query("select love from pic where id='$id'");
$row = mysql_fetch_array($result);
$love = $row['love'];
echo $love;
}
//获取用户真实IP
function get_client_ip() {
if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
$ip = getenv("HTTP_CLIENT_IP");
else
if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
$ip = getenv("HTTP_X_FORWARDED_FOR");
else
if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
$ip = getenv("REMOTE_ADDR");
else
if (isset ($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
$ip = $_SERVER['REMOTE_ADDR'];
else
$ip = "unknown";
return ($ip);
}
?>
connect.php代 码:
$host="localhost";
$db_user="root";
$db_pass="root";
$db_name="50000";
$timezone="Asia/Shanghai";
$link=mysql_connect($host,$db_user,$db_pass);
mysql_select_db($db_name,$link);
mysql_query("SET names UTF8");
?
回复讨论(解决方案)
现在数量可以无刷新变换了,要求是点击的图片也无刷新切换。请高手帮助解决,也可以私下联系我QQ 792785733 ,改好后有酬劳。
原理一样,给img标签换个src而已
问题已解决,不要回贴了,多谢答复