Home › Forums › General Issues › Auto increment number field from front end button
Hi ACF,
I hope someone can help me. I have a custom field called number_attending.
I have a button that a customer will click on the website to say they are attending. Is there an easy way to auto increment this number when the button is pressed? So that when someone is logged in to the backend they can see number of people attending (and also display the number of attendees on the front end, e.g. 4 people are attending this event – the 4 being the auto incremented number).
I hope that someone can help?
Chris
Sure, you can make use of the update_field function. Take a look at the docs to see how it works.
Basically, you can attach some JS to the button which sends of an AJAX request to an action (which you can setup in your functions.php – please see WP docs for this) and then use the get_field function to load the value, and he update_field function to set the new increased value
Thanks
E
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
I can’t give you an exact answer, but I can point out the areas which are most likely to cause an issue.
Firstly. Please debug your code. It is a simple and effective way to find and solve code issues.
1. Does your click event work?
2. Do all JS variables exist? ie. ajaxurl
3. Have you checked your AJAX response in the console log? Any errors?
4. Is DEBUG MODE turned on to show PHP errors?
5. global $post wont work within an ajax call
6. You need to pass the post_id as a variable within data
7. Don’t use get_post_custom
8. Do use get_field
9. Debug your PHP code line by line and make sure it is running correctly.
Thanks
E
The topic ‘Auto increment number field from front end button’ is closed to new replies.
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.