• Resolved Junaid Rashid

    (@sjkjk53j5kl3j)


    I have made a form (selection of books), with multi selection checkbox. I have not added any values to them, because i am planning to show selective data in pdf only.
    I have gone through all the documentation, i easily used this code to make show list of selected items.

    /* Output in a list */
    if ( is_array( $form_data['field'][1] ) ) {
        echo '<ul>';
    
        foreach ( $form_data['field'][1] as $item ) {
            echo "<li>$item</li>";
        }
    
        echo '</ul>';
    }
    

    Then i wanted to pass this data in array and show selective data using if,else statements. I used this code

    <?php 
       $book = $form_data['field'][1];
      
    ?>
    

    Then used this code for initial statement

    
    <p>You have chosen <?php echo $book; ?> Here is the explanation!</p>

    but is showing this statement

    “You have chosen Array Here is the explanation!”

    From where did the array came??
    Can anyone help?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Jake Jackson

    (@blue-liquid-designs)

    Hi Junaid,

    Checkbox fields are stored as PHP arrays in $form_data because users can select more than once answer. That’s why the initial snippet you’ve included above loops through that array to output the data. If you only want a user to select one answer I’d recommend a Radio or Select field instead.

    If you’re happy allowing users to select multiple options, you can convert the array to a string first:

    
    <?php $books = implode( ', ', array_filter( (array) $form_data['field'][1] ) ); ?>
    <p>You have chosen <?php echo $books; ?> Here is the explanation!</p>
    
    • This reply was modified 6 years, 9 months ago by Jake Jackson. Reason: fix PHP snippet
    Plugin Author Jake Jackson

    (@blue-liquid-designs)

    Also, if you’d like to conditionally show content based on the Checkbox field you can do something like this:

    
    <?php if ( in_array( 'Book', (array) $form_data['field'][1] ) ): ?>
        Conditional content
    <?php endif; ?>
    
    Thread Starter Junaid Rashid

    (@sjkjk53j5kl3j)

    Thanx buddy, its done !!! Thanx a lot !!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Array for checkbox’ is closed to new replies.