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:
HTML
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”>
jQuery(‘body’).on(‘click’,’#rsvp_button’,function($){
var data = { action: ‘my_action’ };
$.post(ajaxurl, data, function(response) {
});
});
</script>
For some reason the sets above don’t seem to work.
Any help would be great 🙂
Chris
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
📣 “ACF Chat Fridays”
— Advanced Custom Fields (@wp_acf) January 31, 2023
The ACF team holds their first open office hours this Friday! Come and talk ACF, and ask questions about building sites with the plugin.
We’d love to see you there!
📆 Friday 3rd Feb - 3pm UTC
👉 Register here - https://t.co/3UtvQbE4CU pic.twitter.com/oTwW9K1XQ0
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.