简体   繁体   中英

Pass PHP string variable to Javascript function

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">
            &nbsp;First Floor <button onclick="changeBG(\'floorData2\',\'#F0F\');">Magenta</button>
    </div>';
    echo "<div id='floorData$floorNow'>";

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

Change your Line 5 - Line 7 code to:

echo '<div class="FloorH">
            &nbsp;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

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

Try this

<?php
$floorCount =5;
$floorNow = 1;
while ( $floorNow <= $floorCount)
{
    $floorDiv = 'floorData1';
?>
    <div class="FloorH">
        &nbsp;First Floor <button onclick="changeBG('<?=$floorDiv?>','#F0F');">Magenta</button>
    </div>
    <div id='floorData<?=$floorNow?>'>

    </div>
<?php
    $floorNow = $floorNow + 1;
}
?>
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;
    }

?>

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>

You could do it like this:

    echo "<div class=\"FloorH\">
            &nbsp;First Floor <button onclick=\"changeBG(\"{$floorDiv}\",\'#F0F\');\">Magenta</button>
    </div>";

Or use HereDoc like this:

    echo <<<HTML
    <div class="FloorH">
            &nbsp;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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM