Home › Forums › General Issues › Updating ACF Checkbox Value
I am using ACF and I have extended the user profile so it includes additional textboxes and checkbox values.
On the frontend the user can update their user profile which is working as follows:-
<p class="form-number">
<label for="minimum_annual_salary"><?php _e('Minimum annual salary', 'profile'); ?></label>
<input class="text-input" name="minimum_annual_salary" type="text" id="minimum_annual_salary" value="<?php the_author_meta( 'minimum_annual_salary', $current_user->ID ); ?>" />
</p>
<!-- .form-minimum-salary -->
The problem I have is that I cannot do the same with checkbox values, at the moment I have the below code:-
<p class="form-location">
<label for="interested_in"><?php _e('Desired job title', 'profile'); ?></label><br>
<?php $selected = get_the_author_meta( 'interested_in', $current_user->ID); ?>
<?php $postvalue=array("permanent", "Temporary", "Interim"); ?>
<input type="checkbox" id="chk-permanent" name="interested_in" value="Permanent" <?php if(in_array('permanent', $selected)) { echo 'checked'; } ?> /><label for="chk-permanent"></label><span class="chk-label">Permanent</span><br>
<input type="checkbox" id="chk-temporary" name="interested_in" value="Temporary" <?php if(in_array('Temporary', $selected)) { echo 'checked'; } ?> /><label for="chk-temporary"></label><span class="chk-label">Temporary</span><br>
<input type="checkbox" id="chk-interim" name="interested_in" value="Interim" <?php if(in_array('Interim', $selected)) { echo 'checked'; } ?> /><label for="chk-interim"></label><span class="chk-label">Interim</span>
<input class="text-input" name="interested_in" type="text" id="interested_in" value="<?php echo $postvalue; ?>" />
</p>
<!-- .form-interested-in -->
At the moment, I can get the values for the checkbox and mark the checkbox as checked if they are set already. The problem I have is when I want to update the checkbox values on the frontend.
From what I can see:-
<?php $selected = get_the_author_meta( 'interested_in', $current_user->ID); ?>
returns an array, in my example, if all three checkboxes are checked it will return:-
Array ( [0] => permanent [1] => Temporary [2] => Interim )
For test purposes I tried to update ‘interested_in’ with an array I defined myself as follows:-
$postvalue=array("permanent", "Temporary", "Interim");
But when this gets updated, it sets all the checkboxes to unchecked.
Any ideas on how I can get this working?
Hi @nsilva
ACF has a front-end form feature wich you can use easily. These pages should give you more idea about it: http://www.advancedcustomfields.com/resources/create-a-front-end-form/, http://www.advancedcustomfields.com/resources/acf_form/.
Hope this helps 🙂
You must be logged in to reply to this topic.
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’re reaching out to our multilingual users to ask for help in translating ACF 6.1. Help make sure the latest features are available in your language here: https://t.co/TkEc2Exd6U
— Advanced Custom Fields (@wp_acf) May 22, 2023
© 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.