Cinner Posted September 5, 2007 Share Posted September 5, 2007 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 https://forums.phpfreaks.com/topic/68047-how-to-make-this-simple-change-background-script-dynamic-with-php/ Share on other sites More sharing options...
php_tom Posted September 5, 2007 Share Posted September 5, 2007 <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 https://forums.phpfreaks.com/topic/68047-how-to-make-this-simple-change-background-script-dynamic-with-php/#findComment-342047 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.