AJAX not returning in Wordpress

Robert Granlund
Robert Granlund used Ask the Experts™
I am trying to make an AJAX Call in Wordpress and I am not having any luck.  The console shows all of the returns but they are not being printed out on the page.
jQuery(document).ready(function($) {

	// Listen for a click event on our text input submit button
	$( '.wcsf-submit-field' ).click( function( e ) {

		// Stop the button from submitting and refreshing the page.

		// Now that a click has happened, let's run Ajax!!!!!!!!!
			type: 'POST',
			dataType: 'json',
			url: wcsf_ajax.ajaxurl,
			data: {
				'action' : 'wcsf_ajax',
				//'data' : $( '.wcsf-text-field' ).val(),
				//'submission' : $( '.wcsf-submitted' ).val(),
				//'nonce' : $( '#wcsf-nonce' ).val(),
			success: function( data ) {
				$( '#load_returns' ).html(data);


class WCSF_Example {

     * The construct method. This is where we hook all of our scripts outlined below.
    public function __construct() {
        // Enqueue our ajax with WordPress
        add_action('wp_enqueue_scripts', array($this, 'add_javascript'));
        // Hook our Ajax script with the action we specified in the ajax request
        add_action('wp_ajax_wcsf_ajax', array($this, 'wcsf_ajax'));
        // Add our input/button at the end of the_content() function
        add_filter('the_content', array($this, 'add_button'));

     * Enqueue our JavaScriptssssss
    public function add_javascript() {
        // Enqueue our jQuery. You never know if an install is loading it!
        // Call our script that contains the Ajaxy goodness.
        wp_enqueue_script('wcsf_exmaple_ajax', plugins_url('js/ajax.js', __FILE__));
        // Although used for translation, this function allows us to load arbitrary JS built with PHP into the head of our WP theme
        // Without modifying the themes header.php :) #magix
        // We only need the admin ajax file in WordPress, so that's all we'll do.
        wp_localize_script('wcsf_exmaple_ajax', 'wcsf_ajax', array(
            'ajaxurl' => admin_url('admin-ajax.php'),

     * The actual script to process the Ajax request.
     * This function is called when we pass the "action" key through $_POST and WordPress will map that to the proper add_action().
     * We'll process and then return the data in JSON form. Make sure you sanitize yo data! Safty first.
    public function wcsf_ajax() {
        // Check that we requested this from the input field and the nonce is valid. Prevents malicious users.
        //if (!isset($_POST['submission']) && !$_POST['submission'] && !wp_verify_nonce($_POST['nonce'], 'wcsf-ajax'))
                     global $wpdb;
header('Content-Type: application/json');
        $get_id = $wpdb->get_results('SELECT term_id AS id, name AS name FROM wp_connections_terms WHERE term_id != 1');
        foreach ($get_id as $row) {
            $name = $row->name;
            $id = $row->id;

            $taxid = $wpdb->get_results("
          SELECT wp_connections_meta.meta_value AS value 
          FROM wp_connections_term_relationships, wp_connections_meta 
          WHERE wp_connections_term_relationships.term_taxonomy_id = '$id' AND (wp_connections_term_relationships.entry_id = wp_connections_meta.entry_id)"

            $value = array();
            foreach ($taxid as $row) {
                $val = $row->value;
                $value[] = $val;
            echo 'Sales Manager: ' . $name . '&nbsp;&nbsp;$' . array_sum($value) . '<br /><br />';
         $data = ob_get_clean();  

     * Add our input/button at the end of the_content() function
    public function add_button() {
        echo '<form id="wcsf-example">';
        echo '<input type="text" class="wcsf-text-field" placeholder="Add your text" value="">';
        echo '<input type="submit" class="wcsf-submit-field" value="Add yo text!" />';
        echo '<input type="hidden" name="wcsf-submitted" class="wcsf-submitted" value="true" />';
        wp_nonce_field('wcsf-ajax', 'wcsf-nonce'); // Adds our nonce and creates a unique key automatically! #moremagix
        echo '</form>';


// Initiate our class.
$wcsf_example = New WCSF_Example();

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Distinguished Expert 2017
How the php code is triggered?I mean I cant see any if isset $_POST['action']


I actually figured it out.  I forgot to us ob_start so it ws not looping the way WP wants it to.  Thanks!


Thanks for your help.  I got what I needed.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial