0

Code below has been updated to reflect answer, and is now a working example;

Orignal issue: Below is my code. I built it from the following references but when it runs, I receive no console errors and no errors in my error_log. Nothing is overwritten upon form submit and content is lost on page reload. Thanks in advance for any help.

Javascript (js/custom.js)

$(document).ready(function () {
// AJAX
$( '#um_form' ).submit( function() {
    // Grab our post meta value
    var um_val = $( '#um_form #um_key' ).val();
    $.post(my_ajax_obj, { 
       _ajax_nonce: my_ajax_obj.nonce,     //nonce
        action: "my_tag_count",            //action
        'custom_meta': um_val,       
    });
    // Stop form from submitting
    return false;   
} );
//required post-load event
$( document.body ).trigger( 'post-load' );
}); // end document ready

Functions.php

//AJAX
$ajax_url = admin_url( 'admin-ajax.php' );        // Localized AJAX URL
// Register Our Script for Localization
wp_register_script(
    'custom',                             // Our Custom Handle
    "/wp-content/themes/myTheme/js/custom.js",  // Script URL, this 
script is located for me in `theme-name/scripts/um-modifications.js`
    array( 'jquery' ),                              // Dependant Array
    '1.0',                                          // Script Version ( 
Arbitrary )
    true                                            // Enqueue in Footer
);
// Localize Our Script so we can use `ajax_url`
wp_localize_script( 'custom', 'my_ajax_obj', $ajax_url );
wp_enqueue_script( 'custom' );
}

function my_action() {
// Ensure we have the data we need to continue
if( ! isset( $_POST ) || empty( $_POST ) || ! is_user_logged_in() ) {
    // If we don't - return custom error message and exit
    header( 'HTTP/1.1 400 Empty POST Values' );
    echo 'Could Not Verify POST Values.';
    exit;
}

$user_id = get_current_user_id();
//collect & sanitize
$um_val = sanitize_text_field( $_POST['custom_meta'] );
//update info
update_user_meta( $user_id, 'custom_meta', $um_val );

exit;
}

//add AJAX for logged in users only
add_action( 'wp_ajax_my_tag_count', 'my_action' );

User-profile.php (custom page type)

<?php    
$user_id = get_current_user_id();
//collect data for rendering
$test1 = get_user_meta( $user_id, 'custom_meta', true);
?>
<form id="um_form" method="POST">
    <label for="um_key">
        User Meta Value:
        <input type="text" name="um_key" id="um_key" value="<?php echo $test1;?>" style="width:100%;" />
    </label>
        <input type="submit" value="Submit" />
    </form>
0

The wp_ajax_ is a prefix for your ajax action. your action is my_tag_count so its should be:

add_action( 'wp_ajax_my_tag_count', 'my_action' );

You tried to access the wrong url in the ajax request because you set

wp_localize_script( 'custom', 'my_ajax_obj', $ajax_url );

So the the request url should be my_ajax_obj its not an object its don't have ajax_url in it. So in the request url use only my_ajax_obj.

Another things

Add to the get_user_meta 3rd args to true to get single value instead of array.

$test1 = get_user_meta( $user_id, 'custom_meta', true );

And you don't need to initialze the use meta update will update if exist and if not its will add. so you only need this.

$user_id = get_current_user_id();

$um_val = sanitize_text_field( $_POST['custom_meta'] );

update_user_meta( $user_id, 'custom_meta', $um_val );
  • Ah, thanks! Looks like I'm still experiencing the same issue though, the value isn't being updated on form submit. – Aus-tn Jan 22 '18 at 18:30
  • The ajax code is posted? what is the response? check in the devtools network tab. – Shibi Jan 22 '18 at 18:33
  • Looks like it's returning the entire user-profile pages' HTML source. – Aus-tn Jan 22 '18 at 18:39
  • See my answer I saw another thing... – Shibi Jan 22 '18 at 18:43
  • Thanks, updated my code in my post per your revisions. Same issue (value lost on reload) However it looks like devtools > network is returning the correct value (used 'gggg'); General; Request URL:austinnorris.website/gtl2/user-profile Request Method:POST Status Code:200 OK Remote Address:162.241.244.142:80 Referrer Policy:no-referrer-when-downgrade Form Data; view URL encoded action:my_tag_count custom_meta:gggg – Aus-tn Jan 22 '18 at 19:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.