I have a Javascript function which changes the background color of a DIV ID. I just send it the DIV ID and the Color. changeBG(DivID, Color)
I would like the Div ID to be a variable, so I can set the variable and then call the function. It works fine if I hard code the literal string of the Div ID - 'floorData2" in the example below. I have a PHP variable ($floorDiv) which contains the Div ID and would like to replace 'floorData2' with $floorDiv in the call to the changeBG() function.
I have tried single quotes, double quotes, escaped quotes in every combination I can think of. I still can't make it work. What is the correct syntax to use the variable $floorDiv?
<?php
$floorCount =5;
$floorNow = 1;
while ( $floorNow <= $floorCount) {
$floorDiv = 'floorData1'; /* this will change based on floorNow */
echo '<div class="FloorH">
First Floor <button onclick="changeBG(\'floorData2\',\'#F0F\');">Magenta</button>
</div>';
echo "<div id='floorData$floorNow'>";
echo "</div>";
$floorNow = $floorNow + 1;
}
?>
PHP alternative syntax is much easier for HTML templating
<?php for ($floorNow = 1; $floorNow <= 5; $floorNow++) :
$floorDiv = 'floorData' . $floorNow;
?>
<div class="FloorH">
First Floor
<button value="<?= htmlspecialchars($floorDiv) ?>"
onclick="changeBG(this.value, '#F0F')">Magenta</button>
</div>
<div id="<?= $floorDiv ?>"></div>
<?php endfor ?>
Try this
<?php
$floorCount =5;
$floorNow = 1;
while ( $floorNow <= $floorCount)
{
$floorDiv = 'floorData1';
?>
<div class="FloorH">
First Floor <button onclick="changeBG('<?=$floorDiv?>','#F0F');">Magenta</button>
</div>
<div id='floorData<?=$floorNow?>'>
</div>
<?php
$floorNow = $floorNow + 1;
}
?>
Change your Line 5 - Line 7 code to:
echo '<div class="FloorH">
First Floor <button onclick="changeBG(\'' . $floorDiv . '\',\'#F0F\');">Magenta</button>
</div>';
because PHP will not parse variable inside single quote quoted string .
More detail of variable parsing within double quotes string you can check out this php documentation variable parsing
Yes you can do it easily. suppose you have function.
<script>
function test(param)
{
alert(param);
}
</script>
now to call function.
<script>
test("<?php echo addslashes($a) ; ?>");
</script>
Try this one
<?php
$floorCount =5;
$floorNow = 1;
while ( $floorNow <= $floorCount) {
$floorDiv = 'floorData'.$floorNow; /* this will change based on floorNow */
echo '<div class="FloorH">
First Floor <button onclick="changeBG(\''.$floorDiv.'\',\'#F0F\');">Magenta</button>
</div>';
echo "<div id='floorData".$floorNow."'>";
echo "</div>";
$floorNow = $floorNow + 1;
}
?>
You could do it like this:
echo "<div class=\"FloorH\">
First Floor <button onclick=\"changeBG(\"{$floorDiv}\",\'#F0F\');\">Magenta</button>
</div>";
Or use HereDoc like this:
echo <<<HTML
<div class="FloorH">
First Floor <button onclick="changeBG('{$floorDiv}','#F0F');">Magenta</button>
</div>
<div id='floorData$floorNow'></div>
HTML;
Just as you see,HereDoc is very simple. Hope these could help you and the others.