SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: Extreme Newbie

  1. #1
    SitePoint Member
    Join Date
    Feb 2002
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Extreme Newbie

    anyways. i just started to learn PHP tonight. I have no programming background. ANYWAYS, I thought a good little excersize would be for me to make a page that changes the background on a form submit.



    The Problem

    Since I have the form submitting to itself, I run the script on the same page. BUT, the first time you load up the page, there is no value for $bgcolor .. I thought this would work, but it doesn't seem to fix anything:


    PHP Code:
    <?php 

    if ($bgcolor "") {
    $bgcolor "white";
    } else {
    $bgcolor $color;
    }
    ?>
    it says:
    Warning: Undefined variable: color in background.php on line 18

    uh. hmm.


  2. #2
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why don't you do this:
    PHP Code:
    <?php
    if (isset($submit)) { ?>
    <body style="background-color: <?php echo $color?>;">
    <?php 
    } else { ?>
    <body style="background-color: white;">
    <?php
    }
    ?>
    Sketch
    Aaron Brazell
    Technosailor



  3. #3
    SitePoint Member
    Join Date
    Feb 2002
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    uhm. yea. hehe

    when I add

    <?php if (isset($submit)) { ?>

    it says It could not parse it. and it refers to the last line of the document.

    heres the link of what I'm trying to achieve.

    background.php

    ALSO: what is isset ??
    Last edited by QaBOjk; Feb 14, 2002 at 09:21.

  4. #4
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it should work. If it refers tot he last line chances are you didn't close a curly brace { somewhere. Double check that.

    isset is checks to see if there is a value set for the variable in parentheses. IOW, it checks to see if there is a value for $submit. If your form has <input type="submit" value="submit" name="submit"> then when the script is run after clicking submit, the value for $submit will be set...to "submit". IOW, $submit now IS set and the portion of the script relying on that if statement will be executed. Does that make sense?

    Sketch
    Aaron Brazell
    Technosailor



  5. #5
    SitePoint Zealot
    Join Date
    Jan 2002
    Location
    Launceston, Australia
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just something for you to be aware of - when you first set up PHP, the default is for error reporting to be on (in the php.ini file). I found this to cause quite a few errors on a page when you start using a variable that hasn't been specifically defined yet (ie $color = ''. Change the line in your php.ini file to read

    error_reporting = E_ALL & ~E_NOTICE

    I know the idea is to define your variables first, but hey, not everyone does!

    This might stop some of those nasty little errors appearing!

    Cheers

    Jake

  6. #6
    SitePoint Guru Majglow's Avatar
    Join Date
    Aug 1999
    Location
    B-Town
    Posts
    645
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm, when I used the following to check if a variable is defined, I never had trouble.

    PHP Code:
    $varname == "" 
    and I never changed the ini file. Although, I haven't used that for a while. I use isset now.

    -cARL
    Ohai!

  7. #7
    SitePoint Member
    Join Date
    Feb 2002
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I figured out how todo it

    I used:

    PHP Code:
    <?php 
    if (!isset($color)) {
      
    $color "white";
    }
    ?>
    thanx!!

    the final product is here:
    background.php
    Last edited by QaBOjk; Feb 15, 2002 at 07:11.


Bookmarks

Posting Permissions

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