Home Forums General Issues Auto increment number field from front end button Reply To: Auto increment number field from front end button

  • Could you point me out where this code isn’t working with the update_field option:

    on the button id=”rsvp_button”

    PHP (in the functions.php file)
    function my_action_callback() {

    global $post; // You still may need to pass the POST ID here from the JS ajax.
    // get each post by ID
    $postID = $post->ID;
    // get the post’s custom fields
    $custom = get_post_custom($postID);
    // find the view count field
    $views = intval($custom[‘number_attending’][0]);
    // increment the count
    if($views > 0) {
    update_field($postID, ‘number_attending’, ($views + 1));
    } else {
    update_field($postID, ‘number_attending’, 1, true);
    die(); // this is required to return a proper result
    add_action(‘wp_ajax_my_action’, ‘my_action_callback’);

    JS (in the header.php)
    <script type=”text/javascript”>
    var data = { action: ‘my_action’ };

    $.post(ajaxurl, data, function(response) {

    For some reason the sets above don’t seem to work.

    Any help would be great 🙂