Hertus Hertus - 4 months ago 9
PHP Question

Syntax error in echo out "<a href="

I need to echo this code:

<a href="javascript:toggle('test')"> x Click.</a>


I tried it like this:

if (!empty($item['criteria'])) {
foreach ($item['criteria'] as $item2){
echo "<a href="javascript:toggle('test')>Click</a>";
echo '<div id="'. test.'" style="display: none">'. $item2['description'].'</div>';
}
}


I think there is a mistake with the "".

Answer

You're looking for something like this:

<?php
if (!empty($item['criteria'])) {
    foreach ($item['criteria'] as $item2) {
        echo "<a href=\"javascript:toggle('test');\">Click</a>";
        echo '<div id="' . 'test' . '" style="display: none">'. $item2['description'].'</div>';
    }
}

You need to properly escape your quotes inside strings.