Web Analytics Made Easy -
StatCounter php changing color bg code help - CodingForum

Announcement

Collapse
No announcement yet.

php changing color bg code help

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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>
    My Website
    010100010011110101110100011011110111000001101000

    Comment


    • #3
      doesnt work on my localhost

      Comment


      • #4
        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)

        Comment


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

          Comment


          • #6
            Can someone update the code so that the site remembers the color you last selected?

            Erika

            Comment


            • #7
              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.

              Comment

              Working...
              X