Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Change background image using variable from mysql database

    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.

    Code:
    // 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???

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,316
    Thanks
    23
    Thanked 613 Times in 612 Posts
    In your php you have this line:
    Code:
    $show_image = $path2img. $chosen_image;
    which I think is the image you want to show. AND:
    Code:
    <!--	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:
    Code:
    <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.
    Last edited by sunfighter; 12-21-2011 at 05:05 PM.

  • #3
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thanks - works a treat

    Thanks sunfighter - apparently I'm not allowed to use the "thanks button", which is rather silly. You solved my problem


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •