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 9 of 9
  1. #1
    New Coder
    Join Date
    Feb 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    [FIXED] PHP vars

    I have this:
    Code:
    <select name='bgcolor_highlight' value='<?php echo $bgcolor?>'>
    	<option selected>Red</option>
    </select>
    And on the action page:

    Code:
    <span style=\"background-color: $bgcolor\">Text to be coloured here</span>
    However it does not work, i did the same thing with font colour in the form <font color=\"$color\">Text</color> and that works, why doesnt this?
    Last edited by TaboTabo; 02-24-2007 at 11:31 AM.

  • #2
    Super Moderator JohnDubya's Avatar
    Join Date
    Nov 2006
    Location
    Missouri
    Posts
    634
    Thanks
    12
    Thanked 18 Times in 18 Posts
    If you could give the chunk of code you're using, that would help us know what's going on better.

    But from the looks of it, you're echoing the code or plugging the code into a variable. If that's the case, it's a lot easier to do it like this:

    PHP Code:
    echo '<select name="bgcolor_highlight" value="' $bgcolor '">
        <option selected="selected">Red</option>
    </select>'
    ;

    //and the other codelet

    echo '<span style="background-color: ' $bgcolor '">Text to be coloured here</span>'
    The rule here is if you start with a single quote, you can use double quotes inside...and vice-versa. If you need to use one inside, put a \ before it (looks like you're doing that). Another thing...if you use "", PHP will automatically echo variables within them, while '' will not. That is why in the example I gave you above, since I was using '', I had to stop the ' and use a . to concatenate (link). Then use a . after it and start again with another '. Hope that's clear.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Could be a scoping issue as well.
    To start with this:
    <?php echo $bgcolor?>
    should really have a line terminator on it.
    <?php echo $bgcolor;?>

    Next, the page that this embedded code is located in must have $bgcolor stated as a visible attribute, and is not null.
    Here is how you can tell what the problem is. Check your loaded page source, and if it states your colour as being $bgcolor in the html, its a problem with parsing the php code. That can be done by using an eval statement, but shouldn't be necessary with the one wrapped in the php tags. Next, if the colour is shown as empty, its a problem with variable setting, check to ensure that $bgcolor is set somewhere in the scripting prior to execution.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #4
    New Coder
    Join Date
    Feb 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <span style="background-color: ">

    is in the s. code.

    im rubbish when it comes to PHP but it looks like it is just undifined...?

  • #5
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Your code's only going to work if register_globals is "on", and it is now "off" by default. It used to be on by default, so there's a lot of code out there that assumes it's always on. Put phpinfo(); into your code somewhere then see what it says under register_globals.

    Best thing is to just use $_POST['bgcolor'] (or $_GET['bgcolor']) rather than $bgcolor.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Yes, thats true depening on circumstance. With this one, I'm thinking you are using the value to populate with a select field, which is carried over after the post. That makes this a scoping issue. Use a $_{POST|GET|REQUEST} method to retrieve the data you are looking for if this is the case, usually its post but that would depend completely on your form.
    However, if you are attempting a dynamic populate, perhaps javascript may solve your purposes?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #7
    New Coder
    Join Date
    Feb 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Anyone want to look at the whole page? Its still in rough but if you can be bothered it will help me a lot. I have saved it as .txt so you can download it properly. Also in some of the php tags there is PHP, which i know is incorrect it is just there so at the end i can change it.

    **Removed link**

    Thanks for your help.

    Oh and it has changed from $bgcolor to $bgcolor2

    if you have anything to say about it i will prefer by PM

    Thanks a lot!
    Last edited by TaboTabo; 02-24-2007 at 11:30 AM.

  • #8
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    PHP Code:
    <?php

    /*-----------FIRST GET THE OLD MESSAGES FROM messages.htm-----------*/
    // Read file into an array
    $message_array file("messages.htm");

    // Compile the string
    for ($counter 1$counter 40$counter++) {/*Number of messages to get i.e. show last x messages*/
        
    $old_messages .= $message_array[$counter];
    }

    /*-----------SECOND GET AND MODIFY THE NEW MESSAGE-----------*/
    //ADD TIME
    $time date("H:i");

    //ADD URL
    if($url)
        
    $link_html .= " <?php // <------- why re-opening the PHP tags they are already open! ? ?:S
    chdir('forums'); // path to MyBB
    require './global.php';
    ?><a href='forums/private.php?action=send&uid=$mybbuser[uid]' target=\"_new\"><span style=\"background\-\color: $bgcolor2\"><font color=\"$color\" face=\"wingdings\">$url</font></span></a></a>"
    .
            
    "";
    //ADD name chat
    if($name)
        
    $name_html .="&nbsp; <span style=\"background-color: $bgcolor2\"><font style=\"background-color:$bgcolor2\" color=\"$color\"><b>$name</b></font></span>".
            
    "";
            
    //ADD to name chat
    if($name)
        
    $name_html2 .= "".

  • #9
    New Coder
    Join Date
    Feb 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <------- why re-opening the PHP tags they are already open! ? ?:S
    I said, it is so that i can rewrite ti when i am done. That is the "raw" PHP i need, and when i am done i can rewrite it properly

    Ty for looking, but know why it doent work???

    If you want to see what i am making you know where to click

    Edit works now... THANK YOU FOR YOUR HELP!
    Last edited by TaboTabo; 02-24-2007 at 11:30 AM.


  •  

    Posting Permissions

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