Home Forums General Issues Updating ACF Checkbox Value


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 ); ?>" />
    <!-- .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; ?>" />
    <!-- .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:,

    Hope this helps 🙂

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.