www.webdeveloper.com
Results 1 to 6 of 6

Thread: changing the code of a page.

  1. #1
    Join Date
    May 2006
    Posts
    7

    changing the code of a page.

    Hi I know this is a big but I REALY need to know a way to have someone lets say click a button to change a background, and then when anyone loads that page the new background is what is displayed.

  2. #2
    Join Date
    Jul 2004
    Location
    Sendai, Japan
    Posts
    1,273

  3. #3
    Join Date
    May 2006
    Posts
    7
    that is still just uses cookies, I want the chang to make the change no mater who looks at the page and where they look at the page.

  4. #4
    Join Date
    Oct 2005
    Location
    Gold Coast, Australia
    Posts
    2,115
    To do such your going to need to store the current background image somewhere, for simplicity I've chosen a text file. Below I've constructed a 4-step guide to making a PHP background image switcher that stores it's current image data in a text file named current_bgimage.txt. Currently this script has 3 images (image001.jpg, image002.jpg, image003.jpg) that can be set as the background, you can adjust and add images by editing the Background Imager Changer form
    Step 1.
    Save the following php code as changebgimage.php
    PHP Code:
    <?php
    if(isset($_POST['submit'])) {
        
    $new_bgimage $_POST['bgimage'];
        
    //set this to filename of current background image data
        
    $filename "current_bgimage.txt";
        
        if (!
    $handle fopen($filename'w')) {
             echo 
    'Cannot modify Background!';
             exit;
       }
        
        if (
    fwrite($handle$new_bgimage) === FALSE) {
           echo 
    'Cannot modify Background!';
           exit;
       }
        
    header("Location: ".$_SERVER['HTTP_REFERER']);
        
        
    fclose($handle);
        
    } else
        echo 
    "Sorry, no post variables detected\n";
    ?>
    Step 2.
    Use the following as a template. It's broken into 3 parts,
    PHP Requisition of the current Background Image
    PHP Code:
    <?php
    //set this to filename of current background image data
    $filename "current_bgimage.txt";
    $handle fopen($filename"r");
    $current_bgimage fread($handlefilesize($filename));
    fclose($handle);
    ?>
    The adoption of style (we're focusing on the background-image: url(<?php echo $current_bgimage ?>) part )
    HTML Code:
    <html>
    <head>
    <style type="text/css">
    body {background-image: url(<?php echo $current_bgimage ?>);}
    </style>
    </head>
    <body>
    And finally, the Background Imager Changer form
    HTML Code:
    <!-- Add the following form to anywhere you would like the ability to change the background image -->
    <!-- Please Note: You must update the action path if you add this form to a file in a different directory -->
    <form method="post" action="changebgimage.php">
    <select name="bgimage">
    	<option name="image001.jpg" value="image001.jpg">Change Background Image to: image001.jpg</option>
    	<option name="image002.jpg" value="image002.jpg">Change Background Image to: Image002.jpg</option>
    	<option name="image003.jpg" value="image003.jpg">Change Background Image to: Image003.jpg</option>
    </select>
    <input type="submit" name="submit" value="Change Background Image">
    </form>
    Or alltogether:
    Code:
    <?php
    //set this to filename of current background image data
    $filename = "current_bgimage.txt";
    $handle = fopen($filename, "r");
    $current_bgimage = fread($handle, filesize($filename));
    fclose($handle);
    ?>
    <html>
    <head>
    <style type="text/css">
    body {background-image: url(<?php echo $current_bgimage ?>);}
    </style>
    </head>
    <body>
    
    <!--  Background Imager Changer form -->
    <!-- Add the following form to anywhere you would like the ability to change the background image -->
    <!-- Please Note: You must update the action path if you add this form to a file in a different directory -->
    <form method="post" action="changebgimage.php">
    <select name="bgimage">
    	<option value="image001.jpg">Change Background Image to: image001.jpg</option>
    	<option value="image002.jpg">Change Background Image to: Image002.jpg</option>
    	<option value="image003.jpg">Change Background Image to: Image003.jpg</option>
    </select>
    <input type="submit" name="submit" value="Change Background Image">
    </form>
    <!-- / Background Image Changer form -->
    
    <p>Continue Html...</p>
    
    </body>
    </html>
    Save the above code as test.php

    Step 3.
    Create a text file named current_bgimage.txt and add the name of your default/current background image:
    Code:
    example.jpg
    Step 4.
    Upload the 3 above files along with the background images to your server and open test.php up in your browser. The current background image should be the one you specified above in current_bgimage.txt. Now select a different background image via the Background Imager Changer form and click the Change Background Image button. If you setup my script successfully you should be redirected back to test.php and be presented with a new background image.

  5. #5
    Join Date
    May 2006
    Posts
    7
    You Are The Best Pcthug!!!!!!!!!!!!!!!

  6. #6
    Join Date
    Oct 2005
    Location
    Gold Coast, Australia
    Posts
    2,115
    No worries

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles