Php使用JQUERY AJAX的相似和不同之处


Php Like and Unlike using JQUERY AJAX

我对我的帖子有一个问题。问题是当我点击.like_button时,<span id='you"+New_ID+"'><a href='#'>You</a> like this.</span>没有显示。

我仍然在浏览器开发者控制台上查看。但当我点击点赞按钮时,点赞按钮会改变,但<span id='you"+New_ID+"'><a href='#'>You</a>, </span>没有显示。但如果我刷新页面,<span id='you"+New_ID+"'><a href='#'>You</a>, </span>就会出现。

有人能帮我吗?

我正在使用此代码进行LIKE和UNLIKE:

AJAX查询:

$('.like_button').die('click').live("click", function () {
    var KEY = parseInt($(this).attr("data"));
    var ID = $(this).attr("id");
    if (KEY == '1') {
        var sid = ID.split("likes");
    } else {
        var sid = ID.split("like");
    }

    var New_ID = sid[1];
    var REL = $(this).attr("rel");
    var URL = $.base_url + 'post_like_ajax.php';
    var dataString = 'post_id=' + New_ID + '&rel=' + REL;
    $.ajax({
        type: "POST",
        url: URL,
        data: dataString,
        cache: false,
        success: function (html) {
            if (html) {
                if (REL == 'Like') {
                    $("#elikes" + New_ID).show('fast').prepend("<span id='you" + New_ID + "'><a href='#'>You</a> like this.</span>");
                    $("#likes" + New_ID).prepend("<span id='you" + New_ID + "'><a href='#'>You</a>, </span>");
                    $('#' + ID).html('Unlike').attr('rel', 'Unlike').attr('title', 'Unlike');
                } else {
                    $("#elikes" + New_ID).hide('slow');
                    $("#you" + New_ID).remove();
                    $('#' + ID).attr('rel', 'Like').attr('title', 'Like').html('Like');
                }
            }
        }
    });
    return false;
});

PHP代码:

<?php 
if($login)
{
?>
<a href='#' class='like like_button icontext' id='like<?php echo $post_id;?>' title='<?php echo $like_status;?>' rel='<?php echo $like_status;?>' data=''><?php echo $like_status;?></a> 
<a href='#' class='commentopen commentopen_button icontext' id='<?php echo $post_id;?>' rel='<?php echo $post_id;?>' title='Comment'>Yorum yap </a> 
<?php if($uid != $post_id) { ?>
<?php } } else { ?>
<a href='<?php echo $index_url; ?>' class='like icontext' >Like</a> 
<a href='<?php echo $index_url; ?>' class='commentopen icontext'  title='Comment'>Comment</a>
<a href='<?php echo $index_url; ?>' class='share icontext'  title='Share'>Share</a>     
<?php   
}
?>

<?php if($post_like_count>0) 
{ 
$likesuserdata=$POLL->post_Like_Users($post_id);
if($likesuserdata)
{
echo '<div class="likes" id="likes'.$post_id.'">';
$i=1;
$j=count($likesuserdata);
foreach($likesuserdata as $likesdata)
{
$you="likeuser".$post_id;
$likeusername=$likesdata['username'];
if($likeusername == $session_username)
{
$likeusername='You';
$you="you".$post_id;
}
echo '<a href="#" id="'.$you.'">'.$Wall->UserFullName($likeusername).'</a>';
if($j!=$i)
{
echo ', ';
}
$i=$i+1;
}
if($post_like_count>3)
{
$post_like_count=$post_like_count-3;
echo ' and <span id="like_count'.$post_id.'" class="numcount">'.$post_like_count.'</span> others like this.'; 
}
else
{
echo ' like this.'; 
}
echo '</div>';
}
}
else
{
echo '<div class="likes" id="elikes'.$post_id.'" style="display:none"></div>';
}
?>

post_like_ajax.php

<?php 
include_once 'includes.php';
if(isSet($_POST['post_id']) && isSet($_POST['rel']))
{
$haber_id=$_POST['post_id'];
$rel=$_POST['rel'];
if($rel=='Like')
{
$cdata=$POLL->POST_Like($post_id,$uid); 
}
else
{
$cdata=$POLL->POST_Unlike($post_id,$uid);   
}
echo $cdata;
}
?>

我想您忘记了只显示准备的div,因为一开始您添加了display:none,echo '<div class="likes" id="elikes'.$post_id.'" style="display:none"></div>';

尝试更改这一行:

$("#likes" + New_ID).prepend("<span id='you" + New_ID + "'><a href='#'>You</a>, </span>");

到此:

$("#likes" + New_ID).show().prepend("<span id='you" + New_ID + "'><a href='#'>You</a>, </span>");