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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.