php - jquery - ajax help on wordpress - what's wrong with my code?

Hey all! I'm new to this site and I think I can get help with the code I'm trying to write. I am new to wordpress, php and ajax and writing this simple addon that adds two numbers and shows the result below. I know it's useless; but it will help me learn
// PREPARATIONS - Enqueue All Javascript
function ajax_enqueue_scripts($hook) {
    wp_enqueue_script('adder', plugins_url('/js/adder.js', __FILE__), array('jquery'), '1.0', true);
}
add_action('admin_enqueue_scripts', 'ajax_enqueue_scripts');
// STEP I : Create admin menu
function adder_admin_menu() {
    add_menu_page('ADDER', 'ADDER', 'manage_options', 'adder', 'display_adder_form', 'dashicons-plus', 81);
}
add_action('admin_menu', 'adder_admin_menu');

// STEP II : Display adder form on the menu page created above
function display_adder_form() {
    //END PHP TAG TO WRITE SOME HTML?>
        
        
            

The Ultimate Adder


            
                
                
                
            
        


        
            
        

    //PHP TAG REOPENED
}

function 
ultimate_adder() {
    
$number1 $_POST["number1"];
    
$number2 $_POST["number2"];
    
$result $number1 $number2;
    
$result = (array)$result;
    return 
json_encode($result);
}


?>
And the corresponding JavaScript (adder.js) -

jQuery(document).ready(function($){
    var wpajaxurl = '/wp-admin/admin-ajax.php?action=ultimate_adder';
    $('form.adder_form').bind('submit', function(){
            $form = $(this);
            var form_data = $form.serialize();
            $.ajax({
                'method' : 'post',
                'url' : wpajaxurl,
                'data' : form_data,
                'dataType' : 'json',
                'cache' : false,
                'success' : function (data, textStatus) {
                    $(".answer").html("Answer:" + data["result"]);
                },
                'error' : function (jqXHR, textStatus, errorThrown) {
                    alert("FAIL");
            }
            });
        return false;
        });

});
My code works to a point that I get 'undefined' as my response below my form. Can someone point out what part of it is going bong? Thanks in advance for your time.

Replies

  • Kaustubh Katdare
    Kaustubh Katdare
    I'm no expert in coding; but you may try the following -
    $result = (array)$result;
    return 
    json_encode($result);
    Replace it with
     $result = array('Answer' => $result); 
    die(
    $result);
    I think you'll have to die towards the end of your function.
    Also check if your JS is being called properly on your admin page. That should do the trick. Let us know if it works.

You are reading an archived discussion.

Related Posts

Quote: A Government order in November 2015 has made it mandatory for all road developers in the country to use waste plastic, along with bituminous mixes, for road construction. This...
Quote: Where do you see yourself in five years? Tell me about a time when you showed leadership. What is your biggest weakness? These are the standard questions that job...
Abstract: Energy cannot be created nor be destroyed but it can be converted from one form to another. Nearly all the mass of the atom is concentrated in a tiny...
A team of researchers at the University of Illinois have formulated a new battery design that will separate salt from sea-water, making it suitable for consumption. In addition to that,...
Scientists from the Universities of York, Torino and Connecticut have predicted the morphological structure of self-assembling protein nano-particles by using a mathematical model. Nano particles create symmetrical 3D folds over...