Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

multiple checkboxes in an email enquiry form

Hey there, I have a question - I'm trying to send a contact form and if the user has items in their account they can click multiple checkboxes and it will send the information in the email along with the item names and links

My code:

<?php if ( is_user_logged_in() && have_posts() ) : ?>
<label><?php printf( __( "%s's Item's", APP_TD ), $display_user_name ); ?></label><br>
<?php 
while( have_posts() ) : the_post(); $i++; ?>
<input type="checkbox" name="item_offer" id="item_offer" value="<?php the_title(); ?>"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php endwhile; ?>
<?php else : ?>
<p class="text-center"><?php _e( 'Please login to select your items.', APP_TD ); ?></p>
<?php endif;?>

Now the items show up in the contact form and the contact form sends, but it only shows 1 item checked and not 2 if 2 are select or 3.. it only shows the 1

email.php

$item_offer = appthemes_filter( appthemes_clean( $_POST['item_offer'] ) );

$message .= $item_offer . "\r\n";

How do i display in the email all the checkboxes the user may tick?

I hope someone can help me

Thank You

1 Answer

I found a solution:

I wasn't putting the checkbox into an array first by adding [] = '<input type="checkbox" name="item_offer[]" value="<?php the_title(); ?>">'

and then when I done my message = '$message .= 'Offer: '.implode(' and ',$_POST['item_offer'])."\n";'

It's working and sending ' and ' in between each checkbox selected!

Hey Danny,

I've added some markup to your post so you can see how to best show code. You use three back ticks, which are generally knocking around the bottom left of your keyboard. You can just copy and paste should you wish :)

Thanks!! I was wondering how to do it here!