使用jQuery,Javascript,PHP获取特定的零件类

问题描述:

I'm trying to create a link that functions as a like button.

Currently I have my like link like this:

<a href='javascript:;' title='Like' onClick='like_add_$id'><i class='fa fa-thumbs-up fa-1'></i> Like</a>

And my like.js file is like this:

function like_add(post_id) {
    $.post('ajax/like_add.php', {post_id: post_id}, function(data) {
        if (data == 'success') {
            like_get(post_id);
        } else {
            alert(data);
        }
    });
}

function like_get(post_id) {
    $.post('ajax/like_get.php', {post_id: post_id}, function(data) {
        $('#post_like_'+post_id).text(data);
    });
}

The thing is, as most of you might have already noticed, the post_id variable in the like.js does not hold the actual post_id. The post_id is stored in a PHP variable called $id in the PHP file.

Now, my question is, how would I take that $id from my PHP page and send it to my like.js to store the value in the post_id variable?

Any ideas are much appreciated! Thanks!

You can use hidden inputs :

<input type="hidden" id="postid" value="<?php echo $id; ?>">

Then in your js :

var postid = $("#postid").val();

Also I don't think your onClick will work, it should look like this :

onClick="like_add(<?php echo $id; ?>)"