View Full Version : [FIXED] PHP vars

02-23-2007, 11:20 PM
I have this:

<select name='bgcolor_highlight' value='<?php echo $bgcolor?>'>
<option selected>Red</option>

And on the action page:

<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?

02-23-2007, 11:37 PM
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:

echo '<select name="bgcolor_highlight" value="' . $bgcolor . '">
<option selected="selected">Red</option>

//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.

02-24-2007, 12:11 AM
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.

02-24-2007, 12:26 AM
<span style="background-color: ">

is in the s. code.

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

02-24-2007, 04:43 AM
Your code's only going to work if register_globals (http://us3.php.net/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.

02-24-2007, 06:57 AM
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?

02-24-2007, 11:55 AM
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!

02-24-2007, 12:14 PM

/*-----------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-----------*/
$time = date("H:i");

$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
$name_html .="&nbsp; <span style=\"background-color: $bgcolor2\"><font style=\"background-color:$bgcolor2\" color=\"$color\"><b>$name</b></font></span>".

//ADD to name chat
$name_html2 .= "".

02-24-2007, 12:22 PM
<------- 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 :) (http://fdggfd3.100webspace.net/index_anyone2.htm)

Edit works now... THANK YOU FOR YOUR HELP!