• Resolved bmd91

    (@bmd91)


    I’ve posted on here numerous times about ajax problems but no luck so far. Hopefully someone can help me. I have the following code in a js file:

    //ajax user trade card value to see what they qualify for
    $(document).ready(function(){
    	$("#trade_submit").live('click',function(){
    		var cardAmt = '40';
    		$.ajax({
    			type: 'POST',
    			url: '/wp-admin/admin-ajax.php',
    			data: {
    				action: 'populate_qualified_cards',
    				cardAmt: cardAmt,
    			},
    			success: function(data){
    				console.log(data);
    			},
    			error: function(error){
    				alert(error);
    			}
    		});
    	});
    });

    And have the following in functions.php:

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

    However, in the console where I append the data it is outputting a 0. I’ve tried hard-coding amounts in the php function and it still logs a 0.

    WordPress Gods help!!!!!

    [No bumping, thank you.]

Viewing 7 replies - 1 through 7 (of 7 total)
  • Quick glance, looks right.

    Try moving the two add_actions up above the function definition and name your function something different than the action. i.e.

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

    I have never tried it with the jquery ajax function either, try post if the suggestion above doesn’t work.

    Thread Starter bmd91

    (@bmd91)

    Still doesn’t work. Any other suggestions?

    Ok, this is a working ajax example pulled and stripped down from a site I made.

    I never have been able to get “$” to work in WP, only jQuery(some code). Adapt to your needs:

    Some page:

    <div id="baz">Click me</div>

    A js file:

    jQuery(document).ready(function() {
      jQuery(#baz).click(function() {
        data = {};
        data.action = 'your_ajax_action';
        data.content = 'some content to replace Click Me';
        jQuery.post('/wp-admin/admin-ajax.php',data,your_ajax_callback);
      });
    })
    
    function your_ajax_callback(return) {
      jQuery('#baz').html(return);
    }

    In your plugin/functions.php

    add_action('wp_ajax_your_ajax_action' , 'the_php_ajax');
    add_action('wp_ajax_nopriv_your_ajax_action' , 'the_php_ajax');
    
    function the_php_ajax() {
    // $_POST['content'] == data.content;
      echo $_POST['content'];
      die(); // you must put a die after your echo/print
    }

    Thread Starter bmd91

    (@bmd91)

    That doesn’t work at all. Says ‘unexpected token return’ line 10, which is the ‘function your_ajax_callback(return){‘ line

    Are you doing something with json encoding? I’ve never heard of that error outside of that.

    Thread Starter bmd91

    (@bmd91)

    Not that I am aware of

    Thread Starter bmd91

    (@bmd91)

    Resolved. I have two installs of wp-admin for some reason. Weird… thanks anyway

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Ajax Help’ is closed to new replies.