Simplest ajax form not working when it should

I have been investing some quality time (and headaches) with WP ajax so I understand where you are coming from.

In all honesty, it could be anything from an error in your JS code or something completely bizarre, like:

add_action('wp_ajax_UpdateMeta', 'UpdateMeta');

Should be changed to:

add_action('wp_ajax_updatemeta', 'UpdateMeta');

… to remove the capitals because by some reason, it was causing an issue when I was playing around with adding action to wp_ajax.

The following will work for you, I just tested it on a custom admin page. Note that I added a div with class feedback in case you want to return any data there for testing, or to the user.

html

    <form action="" method="post" id="submitform" />
       <input type="hidden" name="postid" value="<?php echo $post_id; ?>" />
       <input type="hidden" name="action" value="updatemeta" />
       <button type="send" name="submitbtn" id="submitbtn">submit</button>
    </form>
    <div class="feedback"></div>

php

function updatemeta(){

    $post_id = $_POST["postid"];

    update_post_meta($post_id, 'helloworld', $post_id);

    die();
}
add_action('wp_ajax_updatemeta', 'updatemeta');
add_action('wp_ajax_nopriv_updatemeta', 'updatemeta');

JS

jQuery('#submitform').submit(ajaxSubmit);

function ajaxSubmit(){

var submitform = jQuery(this).serialize();

    jQuery.ajax({
        type:"POST",
        url: "/wp-admin/admin-ajax.php", // URL to admin-ajax.php
        data: submitform,
        success:function(data){
            jQuery(".feedback").html(data); // empty div to show returned data
        }
    });

    return false;
    }

Enjoy your beer. 🙂