• Resolved jsheaf

    (@jsheaf)


    I am creating a plugin that has two select menus. When the user selects the first option the second select should populate. This form is placed within a widget.

    I am attempting to use wp_ajax_. The code for the form is in a class:

    class CourseSearch {
    
    	public function __construct( $plugin ) {
    
    		add_action( 'wp_ajax_subject_search', array( $this, 'return_subjects') );
        	add_action( 'wp_ajax_nopriv_subject_search', array( $this, 'return_subjects') );
    	}
    
    	function return_subjects() {
    
    	    $subjects = $this->get_subjects( $_POST['grade'] );
    
    	    echo json_encode( $subjects );
    	}
    }

    Javascript:

    $('#select_grade').on( 'change', function( event ) {
    
    		display_subjects( event );
    	});
    
    /**	Functions
    ------------------------------------------------------------------------*/	
    
    	function display_subjects( event ) {
    
    		var grade = event.currentTarget.value;
    		var path  = plugin.path + 'classes/class-course-search.php';
    
    		console.log( path, grade );
    
    		$.ajax({
    			url: path,
    			type: 'post',
    			dataType: 'json',
    			data: {
    				grade:  grade,
    				action: 'subject_search'
    			},
    			success: populate_subject_list,
    			error: clear_subject_list,
    		});
    	}
    
    	function populate_subject_list( data ) {
    
    		console.log( 'success', this, data );
    	}
    
    	function clear_subject_list( data ) {
    
    		console.log( 'fail', data );
    	}

    I am at a dead end, so any help is appreciated.

Viewing 1 replies (of 1 total)
  • Thread Starter jsheaf

    (@jsheaf)

    Figured it out on my own.

    Simple.

    var path = 'path to admin-ajax.php'

    not

    var path = 'path to my class';

Viewing 1 replies (of 1 total)
  • The topic ‘wp_ajax returns success but data is null’ is closed to new replies.