I have A issue with AJAX in Wordpress. Without AJAX and direct PHP out I can see the Json data. But with the AJAX call the data is not showing.
This is my code
//<![CDATA[
jQuery(document).ready(function($) {
select();
function select(){
var data = {
'action': 'select',
'todo': 'catselect',
};
jQuery.post(ajaxurl, data, function(response) {
$.each(data, function (key, value) {
console.log(value.catname);
});
});
}
});
//]]>
And the PHP code below
<?php
global $wpdb;
$cats_table = $wpdb->prefix . "jb_menu_groups";
function select() {
if($_POST['todo'] == "catselect"){
$resultArray = array();
$connection = mysqli_connect($wpdb->dbhost, $wpdb->dbuser, $wpdb->dbpassword, $wpdb->dbname);
$sql = "SELECT catname, catdescription FROM $cats_table";
$result = mysqli_query($connection, $sql);
header('Content-type: application/json; charset=utf-8');
while($row = mysqli_fetch_assoc($result)){
$resultArray[] = $row;
}
echo json_encode($resultArray);
}
wp_die();
}
add_action('wp_ajax_select', 'select');
?>
I want catname and catdescription as output
Who can help me with this?