WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Ajax Help (8 posts)

  1. bmd91
    Member
    Posted 1 year ago #

    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.]

  2. Andrew Bartel
    Member
    Posted 1 year ago #

    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.

  3. bmd91
    Member
    Posted 1 year ago #

    Still doesn't work. Any other suggestions?

  4. Andrew Bartel
    Member
    Posted 1 year ago #

    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
    }
  5. bmd91
    Member
    Posted 1 year ago #

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

  6. Andrew Bartel
    Member
    Posted 1 year ago #

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

  7. bmd91
    Member
    Posted 1 year ago #

    Not that I am aware of

  8. bmd91
    Member
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic