Php喜欢和使用JQUERY AJAX不同

时间:2014-11-02 22:34:11

标签: javascript php jquery ajax

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

我还在浏览器开发者控制台上查看。但是当我点击Like按钮时,Like按钮会改变,但<span id='you"+New_ID+"'><a href='#'>You</a>, </span>没有显示。但如果我刷新页面<span id='you"+New_ID+"'><a href='#'>You</a>, </span>即将到来。

任何人都可以帮助我吗?

我将此代码用于LIKE和UNLIKE:

AJAX JQUERY:

$('.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;
}
?>

1 个答案:

答案 0 :(得分:1)

我想你忘了只显示你正在预先添加的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>");