...

View Full Version : Change background image using variable from mysql database



fangman
12-21-2011, 09:20 AM
I need to change a background image in a div using a variable defining the pathname, obtained from a mysql query, when the user clicks the submit button on the form.

I have tried using php, jquery, javascript and css to no avail. I am learning programming but obviously am missing something here.


// This gets the chosen tune

$choice = mysql_real_escape_string(@$_POST['choose']);
$chosen = mysql_query("SELECT * FROM toptunes01 WHERE id = $choice");

while(@$row = mysql_fetch_array(@$chosen))
{
$show_tune = $row['id'] . " " . $row['title']. " - ". $row['artist'];
$chosen_id = $row['id'];
$chosen_mp3 = $row['mp3'];
$chosen_title = $row['title'];
$chosen_artist = $row['artist'];
$chosen_image = $row['image'];
$path = htmlentities('/images/toptunes/');
$path2img = htmlentities('/images/toptunes/');

$show_image = $path2img. $chosen_image;
}
?>

<!-- This is the form to choose the tune to play -->

<div id="divChoose">
<form method="post" action="">
<fieldset>
<ul>
<li>
<label for="choose">Select title by number:</label>
<input id="choose" type="text" name="choose" />
</li>
<li>
<input id="choose_button" type="submit" value="Choose" name="choose_button" />
</li>
</ul>
</fieldset>
</form>
</div>

<!-- This is the div to display the image of the cover
styles are in css file
-->

<div id="divImage"></div>

<!-- This is the div to play the mp3 -->

<div id="divPlayer">
<audio controls="controls">
<source src="<?php echo $chosen_mp3;?>" type="audio/mpeg" />
</audio>
</div>

How can I get the divImage to display the image whose filepath is stored in $show_image please please please???

sunfighter
12-21-2011, 05:03 PM
In your php you have this line:

$show_image = $path2img. $chosen_image;which I think is the image you want to show. AND:

<!-- This is the div to display the image of the cover styles are in css file -->
<div id="divImage"></div>

Do this to the display div:

<div id="divImage"><img src="<?php echo $show_image; ?>" /></div>

If you are still having problems it is because your variable is not correct. Echo out $show_image to see what the path and image is. You may need to add "/" in front of it or "./" try both to see if things clear up.

fangman
12-22-2011, 02:21 PM
Thanks sunfighter - apparently I'm not allowed to use the "thanks button", which is rather silly. You solved my problem :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum