0

Im making a like system and am encorporating ajax to make it smooth. Everything works okay except it always defaults to the last post in for loop. My thinking is there is no way for the javascript to know which element of id "like" to post to.

main.js:

   $(".like>a").click(function() {

    $.post(base_url + "index.php/userprofile/like_post/", { post : post }, function(data) {

        alert('liked');

    }, "json");

    return false;

});

Im passing through the post variable from the view file. I grab the postID of each post.

userprofile_view.php:

            <?php foreach ($posts as $post) 
            { ?>
            <?php $postID = $this->model_posts->getPostData('id', $post->post); ?>
            <script type="text/javascript">
                var post = "<?php echo $postID; ?>";
                var base_url = "<?php echo base_url(); ?>";
            </script>   

model_posts.php:

function likePost($post) {
    $data['user_ID'] = $this->session->userdata('id');
    $data['post_liked'] = $post;

    $insert = $this->db->insert('user_post_likes', $data);
    return $insert;
}

userprofile.php(controller):

    public function like_post() {
    $this->load->model('model_posts');

    $post = $this->input->post('post');

    $this->model_posts->likePost($post);
}

If someone couldhelp me out that would be great!

6
  • what is the post in the click handler... you need to share more context to that Commented Oct 18, 2014 at 3:50
  • the variable that im specifiying in the view Commented Oct 18, 2014 at 3:53
  • whether $(".like>a").click(function() { is done in a loop? Commented Oct 18, 2014 at 3:54
  • where is the .like > a element gets created.... the problem is you are creating a global variable post in a loop.. so its value is getting overriden every time.. thus it will have only the last value Commented Oct 18, 2014 at 3:56
  • Ive came to that conclusion, I just dont know how to fix it Commented Oct 18, 2014 at 4:01

3 Answers 3

1

The problem is your usage of a global variable in a loop, so the variable will have only the last value of the loop.

You can use a data-* attribute like

<script type="text/javascript"> 
var base_url = "<?php echo base_url(); ?>"; 
</script>   

<?php foreach ($posts as $post) 
{ ?> 
<?php $postID = $this->model_posts->getPostData('id', $post->post); ?> 

<div class='posts'> 

<div class='posts_img'> 
<img src="<?php echo base_url() . 'img/profilepictures/thumbs/' . $profilepicture?>"> 
</div> 

<div class='posts_user'>    
<strong><?php echo $prefname; ?></strong>   
</div>  

<div class='posts_date'> 
<?php echo $this->model_posts->getPostTime($post->post); ?> 
</div> 

<div class='post'> 
<p><?php echo $post->post ?></p> 
</div> 

<?php if($this->model_posts->doesUserLike($me, $postID)) { ?> 
<div class='unlike'> 
<?php echo anchor('userprofile/unlike_post/' . $me . '/' . $postID, 'unlike'); ?> 
</div> 
<?php } else { ?> 
<div class='like' data-post="<?php echo $postID; ?>"> 
<?php echo anchor('#', 'like', array('id' => 'like')); ?> 
</div> 
<?php } ?>

then

$(".like>a").click(function () {

    var post = $(this).parent().attr('data-post'); 
    $.post(base_url + "index.php/userprofile/like_post/", {
        post: post
    }, function (data) {

        alert('liked');

    }, "json");

    return false;

});
Sign up to request clarification or add additional context in comments.

Comments

0

if you're sending same ID field with different values stop, send unique IDs with selected values OR send ID with values as post array, PHP can deal with it

Comments

0
<script type="text/javascript">
    var post = "<?php echo $postID; ?>";
    var base_url = "<?php echo base_url(); ?>";
</script>

This is your problem. You're declaring these variables in global scope. So every time your PHP foreach loop iterates, you're redefining the variable in global scope, overwriting the previous value.

Instead, set an id attribute on each <a> tag to be the $postID, and get that ID in your click handler, like so:

$(".like>a").click(function() {
    var post = this.id;
    $.post(base_url + "index.php/userprofile/like_post/", { post : post }, function(data) {
        alert('liked');
    }, "json");

    return false;
});

You would have to modify the code that creates the <a> tags to include the id attribute with the $postID value assigned to it...I don't see that part included in your code samples.

1 Comment

Yea how can I fix that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.