Support

Account

Home Forums Front-end Issues Check if Multiple Checkbox is checked in front end

Solving

Check if Multiple Checkbox is checked in front end

  • I have multiple checkbox input in front end form, i want to show which input is checked from an user.

    I try this:

    <?php $servizi = get_field(‘servizi’); ?>

    <?php
    $servizi = array($servizi); ?>

    Services

    <div><input type=”checkbox” <?php if ( $servizi && in_array(array(‘servizio1’), $servizi) ): ?> checked=”checked” <?php endif;?>
    <div><input type=”checkbox” <?php if ( $servizi && in_array(array(‘servizio2’), $servizi) ): ?> checked=”checked” <?php endif;?>
    <div><input type=”checkbox” <?php if ( $servizi && in_array(array(‘servizio3’), $servizi) ): ?> checked=”checked” <?php endif;?>

    not working

    can you tech me how to do? thanks

  • First a checkbox field returns an array so this is not needed

    
    $servizi = array($servizi);
    

    Second, your in_array() arguments are wrong. $needle is a string, not an array https://www.php.net/manual/en/function.in-array.php

    
    in_array('servizio3', $servizi) 
    
  • Thanks! i solved my issue 🙂
    This my code:
    <?php
    $services = get_field(‘servizi’);
    ?>
    <div class=”tab services”>
    <div><input <?php if( $services && in_array(‘value1’, $services) ) {
    echo “checked”;}?> type=”checkbox” name=”servizi[]” value=”value1″ /> Value 1</div>

    in my case so the user can modify his service AD from the custom form in the frontend and he finds the checkbox already selected to edit.

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

You must be logged in to reply to this topic.