function post_like(){
global $wpdb;
$c = $_POST['next'];
echo $c;
$d = get_the_ID();
$wpdb->insert('wp_likes',
array(
'article_id' => $d,
'user_id' => '1'
)
);
die();
}
and my ajax.js file:
function Click_Me() {
var className = jQuery('#flag').attr('class');
if (className == "like") {
var temp = "unlike";
} else {
var temp = "like";
}
jQuery.ajax({
url: like.ajax_url,
dataType: 'html',
data: {
action: 'post_like',
next: temp
},
success: function() {
jQuery('#flag').attr('class', temp);
jQuery('#sonuc').html(temp);
}
});
}
If I want to get id with this method
$a = get_the_ID();
$a variable's value == 0
Why? I tried other tricks for getting id, but methods do not works. I think the problem is ajax but I don't understand why... Thanks for answers.
You can make a global post and fetch its id. Like:
global $post;
$d = $post->ID;
If you don't want to do it this way. You can simply pass the id value to ajax data.
If your user id returns 0, you should be use this metod : intval();
Because during wordpress coding, table type int : %d so it's must be integer.
For Example(Ajax) :
$user_id = intval( $_POST['user_id'] );
You can simply create a function with global $post as an argument.
function post_like($post){
global $wpdb;
$c = $_POST['next'];
echo $c;
$d = $post->ID
$wpdb->insert('wp_likes',
array(
'article_id' => $d,
'user_id' => '1'
)
);
die();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.