1

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;
    }
    ?>
3
  • echo '<button onclick="changeBG(\'' . $floorDiv . '\',\'#F0F\');">'; Commented Jun 16, 2016 at 5:51
  • also have a look at HEREDOC! That syntax reduces problems like that. Commented Jun 16, 2016 at 5:52
  • Thanks Jeff. That worked great. I should of asked the question 4 hours ago. Commented Jun 16, 2016 at 6:23

6 Answers 6

2

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

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

Thank You Phil. That code is much cleaner than mine. It will take me a few more edits than Jeff's solution, but I will check out the alternative syntax.
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;
    }

?>

Comments

1

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

2 Comments

Needs <div id="floorData<?= $floorNow ?>">
Thanks :) @Phil i missed that.
0

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>

2 Comments

What if $a contains quote characters, eg $a = 'I have "quote" characters';?
then you can use addslashes. i am updating the code.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.