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

Chris Bell
Chris Bell
9,125 Points

php help

Anyone have any idea why this echo statement is not working?

    $var1 = $catagory["catagory_id"]; //This is 3 below
    $var2 = $catagory["name"];  //This is Hoodies below

    echo '<button onclick="getCatagory("'. $var1.'")">'. $var2 . '</button>';

The output is this: < button 3")"="" onclick="getCatagory(">Hoodies< /button >

I've tried at least 10 different ways of doing this, all have the same output

2 Answers

Stone Preston
Stone Preston
42,016 Points

concatenation can get a bit messy when having to use quotes. in those cases I would probably just use php tags instead. Something like this should work

   <button onclick="<?php echo getCatagory($var1)?>"><?php echo $var2; ?></button>;

but if you really wanted to concatenate maybe try

echo '<button onclick="' . getCatagory($var1) . '">' . $var2 . '</button>';