I'm trying to get a value from a PHP function through Ajax and save it in a variable in jquery.
The PHP;
add_action( 'wp_ajax_mark_as_read', array( &$this, 'readen_color' ) );
public function readen_color() {
if( isset( $_POST['post_id'] ) && is_numeric( $_POST['post_id'] ) ) {
echo 'hello';
}
}
The Jquery:
$(document).ready(function(){
alert ('Test');
var sArticleId, iPostId;
// Get the article ID and split it -
sArticleId = $("article").attr('id');
iPostId = parseInt(sArticleId.split('-')[1]);
$.post(ajaxurl, {
action:'readen_color',
post_id: iPostId
}, function (response) {
console.log(response);
var response = parseInt(response);
alert('value of response: ' + response);
});
});
Any ideas what i'm doing wrong?
Could not answer the my own question but Now it works with:
var responsevar = response;
alert('The Server Responses: ' + responsevar);
Instead of:
var response = parseInt(response);
alert('value of response: ' + response);
wp_ajax_nopriv_(action)
for ajax on a public page.