Pass PHP string variable to Javascript function

Solution:1

PHP alternative syntax is much easier for HTML templating

<?php for ($floorNow = 1; $floorNow <= 5; $floorNow++) :
      $floorDiv = 'floorData' . $floorNow;
?>
    <div class="FloorH">
        &nbsp;First Floor
        <button value="<?= htmlspecialchars($floorDiv) ?>" 
                onclick="changeBG(this.value, '#F0F')">Magenta</button>
    </div>
    <div id="<?= $floorDiv ?>"></div>
<?php endfor ?>

Solution:2

Try this one

<?php
$floorCount =5;
    $floorNow = 1;
    while ( $floorNow <= $floorCount) {
        $floorDiv = 'floorData'.$floorNow; /* this will change based on floorNow */
    echo '<div class="FloorH">
            &nbsp;First Floor <button onclick="changeBG(\''.$floorDiv.'\',\'#F0F\');">Magenta</button>
    </div>';
    echo "<div id='floorData".$floorNow."'>";

    echo "</div>";
    $floorNow = $floorNow + 1;
    }

?>