使用ajax获取内容并直接显示它们而无需刷新页面 [英] get contents with ajax and display them directly without refreshing the page

查看:100
本文介绍了使用ajax获取内容并直接显示它们而无需刷新页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在php中创建了一个博客.对于每个评论,用户可以按收藏夹/不收藏夹"按钮(如果需要)来收藏/取消收藏此帖子.我的按钮很完美.我得到的唯一问题是,当用户按下收藏夹/不喜欢的..."时,我没有获得此帖子的收藏夹/不喜欢的"数量.为了得到这个,我每次必须重新整理页面.有人告诉我,我需要使用Ajax才能做到这一点.

I have created a blog in php. For each comment the user can press a Favorite/Unfavorite button (if want) to Favorite/Unfavorite a post. My button works perfect. The only problem I got is that when user press Favorite/Unfavorite... I dont get the number of Favorites/Unfavorites for this post. In order to get this, each time I have to refressh the page. Some people told me that I need to use Ajax in order to do this.

我使用喜欢表,以保留每个帖子的收藏夹:喜欢(like_id,用户,the_comment_id) 我在每个帖子中都使用表格注释:评论(comments_id,评论,用户)

I use table likes, to hold favorites for each post: likes(like_id, user, the_comment_id) I use table comments for aeach post: comments(comments_id, comment, user)

这是我的php代码:

<?php

$comment_id = $row['comments_id'];

// ... code above

//button for favorite and unfavorite
 $get_button = mysql_query(" SELECT * FROM `likes` WHERE `user`='$session_user_id' AND `the_comment_id`='{$row['comments_id']}'  ");                    
 $get = mysql_fetch_assoc($get_button);

if($get==""){   
    $comments .= "<a role='button' class='button' id='like$comment_id'  style='color:grey;'>Favorite</a>";
}else if($get!=""){
    $comments .= "<a role='button' class='button' id='unlike$comment_id' style='color:grey;'>Unfavorite</a>";
}

// place favorites for this comment here
$comments .= " $total_favorites ";

?>

这是我的jquery:

This is my jquery:

<script>

$(document).ready(function(){
$("#like<?php echo $comment_id; ?>").click(function() { 
  var id = "<?php echo $comment_id; ?>";
  $.post("parse.php",{like:id}, function(data){

   $("#like<?php echo $comment_id; ?>");
   $(".button<?php echo $comment_id; ?>").html(data);

  });
  $(this).hide().attr("Disabled", "True").text("Favorite done!").show();
});


$("#unlike<?php echo $comment_id; ?>").click(function() { 

  var id = "<?php echo $comment_id; ?>";
  $.post("parse.php",{unlike:id}, function(data){

   $("#unlike<?php echo $comment_id; ?>");
   $(".button<?php echo $comment_id; ?>").html(data);

});     
$(this).hide().attr("Disabled", "True").text("Unfavorite done!").show();
 });

});

</script>

这是我的parse.php代码:

This is my parse.php code:

<?php  

if(isset($_POST['like'])){
 $id = $_POST['like'];      
  mysql_query("INSERT INTO likes VALUES ('', '$session_user_id', '$id') "); 
}


if(isset($_POST['unlike'])){
 $id = $_POST['unlike'];
 mysql_query(" DELETE FROM likes WHERE `user`='$session_user_id' AND `the_comment_id`='$id'  ");
}


$favorites = mysql_query(" SELECT * FROM `likes` WHERE `the_comment_id`='{$row['comments_id']}'  ");    
$total_favorites = mysql_num_rows($favorites);

?>

推荐答案

您需要从parse.php脚本返回一些内容.除非您直接回显它或返回JSON并在设置.html(data)值之前在jQuery函数中进行解析,否则data变量将不包含该计数.

You would need to return something from the parse.php script. The data variable will not contain the count unless you either echo it out directly or return JSON and parse in your jQuery function prior to setting the .html(data) values.

这篇关于使用ajax获取内容并直接显示它们而无需刷新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆