WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Admin-ajax returns a 0 (4 posts)

  1. bmd91
    Member
    Posted 1 year ago #

    I am trying to write a simple ajax call that grabs the value of an input on blur and returns it. The ajax call is in a folder called 'scripts' in my themes directory and the function is in functions.php. My code is as follows:

    $(document).ready(function(){
    	$("#trading_card_amount").blur(function(){
    	var cardAmt = $(this).val();
    	$.ajax({
    		type: 'POST',
    		url: '/../../../../wp-admin/admin-ajax.php',
    		data: {
    			action: 'populate_qualified_cards',
    			cardAmt: cardAmt,
    		},
    		success: function(data){
    			console.log(data);
    		},
    		error: function(){
    			console.log('error');
    		}
    	});
    });

    and in functions.php i have:

    function populate_qualified_cards(){
        $amt = $_POST['cardAmt'];
        echo $amt;
        die();
    }
    add_action( 'wp_ajax_nopriv_populate_qualified_cards', 'populate_qualified_cards' );
    add_action( 'wp_ajax_populate_qualified_cards', 'populate_qualified_cards' );

    Any ideas why I only get a zero in the console log? Help please!!!!!

  2. bmd91
    Member
    Posted 1 year ago #

    found a work around

  3. BeGe
    Member
    Posted 1 year ago #

    And your workaround was???????

  4. bmd91
    Member
    Posted 1 year ago #

    Make sure action and function are not called the same thing

Topic Closed

This topic has been closed to new replies.

About this Topic