• The Big K

    (@crazyengineers)


    Hello All!

    I’m trying to learn coding for WordPress and my first addon is expected to create a simple addition form :

    [Enter Number 1 ] [Enter Number 2] [Calculate]
    Result: XYZ

    Note: XYZ = Number1 + Number2.

    Clicking on the Calculate button should update the XYZ part with the result of addition of the numbers entered. I’m only halfway through the code, and stuck. Not sure how should I proceed. Would really appreciate your help. Thanks!

    My Code:

    http://pastebin.com/4NeXmHBf

    [Large code excerpt removed by moderator per forum rules. Please use Pastebin for all large code excerpts. It works better anyway.]

Viewing 2 replies - 1 through 2 (of 2 total)
  • I looked at your pastebin code and you cannot do it this way since it is not secure. Why does your form have an action to /wp-admin/admin-ajax.php?action=kk_adder when you are implementing ajax and jSON? I think javascript code is better for this and there would be no need for WordPress API code or PHP, but I see that you are trying to learn hook programming.

    You have 2 action hooks that are not in the WordPress API:
    add_action(‘wp_ajax_nopriv_kk_adder’, ‘kk_adder’);
    add_action(‘wp_ajax_kk_adder’, ‘kk_adder’);

    The first parameter should be part of the WordPress API, not made up. The second parameter is correct, the function call name.

    Instead of all this extra code, you may want to look into the hook wp_enqueue_script() which is how javascript is implemented.

    This code would make more sense:

    /**
     * Proper way to enqueue scripts and styles
     */
    function theme_name_scripts() {
    	wp_enqueue_style( 'style-name', get_stylesheet_uri() );
    	wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/kk_adder.js', array(), '1.0.0', true );
    }
    
    add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );

    Then, in your theme. add a JS directory and write a javascript file kk_adder.js.

    Doing it this way, you will still get experience writing WP hooks and add security.

    Hope this helps.

    Moderator bcworkz

    (@bcworkz)

    The first parameter should be part of the WordPress API, not made up.

    hotwebideas – true, but the actions in question are not made up in the manner you think. You are apparently unfamiliar with WP AJAX coding. The actions are dynamically created based on the passed ‘action’ parameter, they are actually fine as-is. You might be interested in the link below 😉

    Big K – despite hotwebideas being a bit off on action tags, he’s otherwise made good comments. The code as-is is insecure, but that can be easily fixed. You of course don’t need the WP API to do math problems, but it’s safe to assume you’ll eventually do something more practical that does require WP once you figure out this basic stuff 🙂

    As you know, the admin-ajax.php is the correct destination for an AJAX call, but you do not use the form action attribute to get there. The action can simply be "#" because you will be using javascript or jQuery to make the request. Specifying an action attribute will cause the browser to try to load a new page, defeating the AJAX concept.

    So you do need to enqueue javascript to do this. It’ll fire upon form submission to make the AJAX request, then use the returned value to update the content of some HTML container on the page.

    More information on using AJAX in the WP environment can be found starting here:
    https://developer.wordpress.org/plugins/javascript/

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Creating A Simple Addition Form In Admin Dashboard’ is closed to new replies.