Jump to content

how to make this simple change-background script dynamic with PHP?


Cinner

Recommended Posts

I downloaded this simple Javascript that changes the background of an element when you press a button. Like this:

<script type="text/javascript">
  function ChangeBackground(background)
  {
    switch (background)
    {
      case 1:
        document.getElementById('myTableCell').style.backgroundImage = 
              'URL(/Images/Demos/ChangeBackground/Background1.gif)';
        break;
      case 2:
        document.getElementById('myTableCell').style.backgroundImage = 
               'URL(/Images/Demos/ChangeBackground/Background2.gif)';
        break;
      }
    }
  </script>

 

Then you give the button this simple property: onclick="ChangeBackground(1)" or "ChangeBackground(2)".

 

But the thing is, I have a list of items that come from a mysql datase with php, and each item has it's own two background images a user can choose from. Those images are stored as URL's in the database. Is there a way I can pass those URL's through the buttons along with the "1" and "2"? Something like ChangeBackground(1,url1). My javascript knowledge is very limited I'm afraid.

<script>
var urls = Array();
<?php
$res = mysql_query("SELECT * FROM bgimages");
$c = 0;
while($row = mysql_fetch_assoc($res)) {
  echo "urls[$c] = ".$row['url'];
  $c++;
}
echo "function ChangeBackground(background) {";
echo "document.getElementById('myTableCell').style.backgroundImage = urls[$i];";
echo "}";
?>
</script>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.