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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jul 2002
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy php changing color bg code help

    im trying to make a page that change color background by clicking on a link. however, it doesn't work. why?

    PHP Code:
    <?php
        
    if(!isset($_POST[color])) $_POST[color] = 'green';
    ?>

    <html>
    <head>
    <title>Head</title>
    </head>
    <body bgcolor=<?php print "$_POST[color]";?>>
    Welcome!

    <a href="<?php echo $_SERVER[PHP_SELF];?>?color=red">red</a>
    <a href="<?php echo $_SERVER[PHP_SELF];?>?color=blue">blue</a>

    </body>
    </html>
    can any show me a more better code to do it? thanks.

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    the Netherlands
    Posts
    215
    Thanks
    0
    Thanked 0 Times in 0 Posts
    How about this then?

    Code:
    <?php
        if(!isset($color)) $color = 'green';
    ?>
    
    <html>
    <head>
    <title>Head</title>
    </head>
    <body bgcolor=<?php print "$color";?>>
    Welcome!
    
    <a href="<?php echo $_SERVER[PHP_SELF];?>?color=red">red</a>
    <a href="<?php echo $_SERVER[PHP_SELF];?>?color=blue">blue</a>
    
    </body>
    </html>

  • #3
    New to the CF scene
    Join Date
    Jul 2002
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    doesnt work on my localhost

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,096
    Thanks
    11
    Thanked 101 Times in 99 Posts
    Hi, you are sending the variables via the url so you need to use HTTP_GET_VARS i.e.

    PHP Code:
    <?php
        
    if(!isset($_GET[color])) $_GET[color] = 'green';
    ?>

    <html>
    <head>
    <title>Head</title>
    </head>
    <body bgcolor=<?php print "$_GET[color]";?>>
    Welcome!

    <a href="<?php echo $_SERVER[PHP_SELF];?>?color=red">red</a>
    <a href="<?php echo $_SERVER[PHP_SELF];?>?color=blue">blue</a>

    </body>
    </html>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #5
    New to the CF scene
    Join Date
    Jul 2002
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    thank you for helping a php newbie. u are the man.

  • #6
    Registered User
    Join Date
    Aug 2003
    Location
    Virginia, USA
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Can someone update the code so that the site remembers the color you last selected?

    Erika

  • #7
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    577
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    <?php
    session_start
    ();
    $_SESSION['color'] = ((isset($_GET['color'])) ? $_GET['color'] : (isset($_SESSION['color']) ? $_SESSION['color'] : 'green')); // default
    ?>
    <html>
    <head>
    </head>
    <body bgcolor=<?php echo $_SESSION['color']; ?>>
    etc....
    <?php
    $colors 
    = array('red','green','blue','yellow','black','white','gold');
    foreach(
    $colors AS $value)
        {
        echo 
    '<a href="?color=' .$value'">' .$value'</a> ';
        }
    ?>
    </body>
    </html>
    that probably over complicates things a tad - basically we're just storing the color as a session variable now, which bases its value on the existence of the get variable.
    if the ternery (a=b) ? true : false; conditional interests you, there is a tutorial here
    hopefully you'll be able to suss the foreach looping yourself.
    Ökii - formerly pootergeist
    teckis - take your time and it'll save you time.


  •  

    Posting Permissions

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