Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

Danny Massa
Danny Massa
27,856 Points

Send <a href=”#”> through checkbox value

Is it possible to send an HTML ancor tag through a checkbox value?

e.g. < input type="checkbox" name="item_offer[]" value="< a href="#" >iPad Air 32GB< /a >" >

Then that value be send through email message as a link? That way when the user receives an email he can click the link and take them to the page?

7 Answers

Jeremy Germenis
Jeremy Germenis
29,854 Points

Instead of trying to pass the href tag you can do something like take the value="iPad Air 32GB" and have php add the href tags to the value.

Example: $link = '<a href="#">' . $_POST[item_offer] . '</a>';

Just to expand a little more, you'll want to do this in the form processing script - wherever that may be. When the form is submitted the post-processing script can wrap the checkbox value just like Jeremy Germenis suggested and save it in a variable. When you build the body of the email, you can then concatenate the whole link element within the body - making sure you also add spaces either side of the variable! :-)

Danny Massa
Danny Massa
27,856 Points

Sweet! Thanks for the reply

I have the href in a while loop

<?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;?>

if the client selects 2 checkboxes or 3 checkboxes then there are 3 href links - how do i assign them according to each checkbox they selected

Jeremy Germenis
Jeremy Germenis
29,854 Points

You would set the attribute name to "item_offer[]" which will create an array of item_offers's. You can then traverse through $_POST[item_offers] as an array with a foreach loop.

Danny Massa
Danny Massa
27,856 Points

awesome! Thank you so much jeremy, anyway you can show me an example? :)

Jeremy Germenis
Jeremy Germenis
29,854 Points
<input name="item_offer[]" value ="http://something.com" />
<input name="item_offer[]" value ="http://something-else.com" />

foreach ($_POST['item_offer'] as $value ) {
  echo '<a href="' . $value . '">click here for awesome</a>'; 
}
Danny Massa
Danny Massa
27,856 Points

Hmmm, I'm having trouble cause I have

the_title(); - (title of the item in persons inventory) and the_permalink(); - (the link to that item)

the value of the checkbox is the item name e.g. 42" samsung plasma and emailed but I want the the link aswell in the email so ppl can just click the direct link to the item

I can't add the_permalink(); in the value of the checkbox with a href with the title though that would be easy done

<input type="checkbox" name="item_offer[]"  value="<?php the_title(); ?>"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>

So i need the link and the name - please help me :(

Jeremy Germenis
Jeremy Germenis
29,854 Points

The title would depend on your structure.

  1. You could "explode" the value -> value="example.com+MyTitle" (google search "php explode")
  2. If your products are in a database you could query the db for the title
  3. If you are using jQuery you could add another attribute