Please help. I have a simple plugin which uses ajax call. When I call another function within ajax php function (in the below example I am calling get_cat() function from update_passage_list() function) I am getting "Failed to load resource: the server responded with a status of 500 (Internal Server Error)" error.
quiz.js:
$jq =jQuery.noConflict();
$jq(document).ready(function($jq) {
update_passage_list(32);
});
var update_passage_list = function(id){
$jq.post(quiz.ajaxURL, {
action:"update_passage_list",
nonce:quiz.quizNonce,
cpid : id
}, function(return_data) {
alert('Return Data = ' + return_data);
}, "json");
};
index.php:
<?php
/*
Plugin Name: WP Quiz
Plugin URI: -
Description: Plugin description.
Author: Author Name
Version: 1.0
Author URI: url
*/
class WP_Quiz {
public $plugin_url;
public function __construct() {
$this->plugin_url = plugin_dir_url(__FILE__);
add_action('admin_enqueue_scripts', array($this, 'wpq_admin_scripts'));
add_action('wp_ajax_nopriv_update_passage_list', array($this, 'update_passage_list'));
add_action('wp_ajax_update_passage_list', array($this, 'update_passage_list'));
}
function wpq_admin_scripts() {
wp_enqueue_script('jQuery');
wp_register_script('quiz-admin', plugins_url('js/quiz.js', __FILE__), array('jquery'));
wp_enqueue_script('quiz-admin');
$config_array = array(
'ajaxURL' => admin_url('admin-ajax.php'),
'quizNonce' => wp_create_nonce('quiz-nonce'),
'plugin_url' => $this->plugin_url
);
wp_localize_script('quiz-admin', 'quiz', $config_array);
}
public function get_cat($id) {
$new_id = $id + 100;
return $new_id;
}
function update_passage_list() {
$cpid = $_POST['cpid'];
$cpid = get_cat($cpid);
echo $cpid;
die();
}
}
$quiz = new WP_Quiz();