...

View Full Version : Variables and Values



oztinks
04-18-2004, 11:49 PM
Hi,
This is my first attempt at php, I have a form where the user selects colors on page 1 those colors are printed on the second page as hidden fields that works fine but how do I pass those values onto the select fields?

When I use the below form it returns

col1 instead of #A6A6A6

I have tried changing the select values to $col1 and $_GET[col1]
But I am obviously looking at this the wrong way

Any help much appreciated



<FORM ACTION="" METHOD="GET" TARGET="<?php $PHP_SELF ?>" >
<input type="hidden" name="col0" id="col0" value="#808080" />
<input type="hidden" name="col1" id="col1" value="#A6A6A6" />
<input type="hidden" name="col2" id="col2" value="#CCCCCC" />
<input type="hidden" name="col3" id="col3" value="#7F7F7F" />
<input type="hidden" name="col4" id="col4" value="#A6A6A6" />
<input type="hidden" name="col5" id="col5" value="#CCCCCC" />
<input type="hidden" name="col6" id="col6" value="#7F7F7F" />
<input type="hidden" name="col7" id="col7" value="#7F7F7F" />
<input type="hidden" name="col8" id="col8" value="#000000" />
<select name="body[color]">
<option value="">None</option>
<option value="col0">1</option>
<option value="col1">2</option>
<option value="col2">3</option>
<option value="col3">4</option>
<option value="col4">5</option>
<option value="col5">6</option>
<option value="col6">7</option>
<option value="col7">8</option>
<option value="col8">9</option>
</select>
<select name="div[color]">
<option value="">None</option>
<option value="col0">1</option>
<option value="col1">2</option>
<option value="col2">3</option>
<option value="col3">4</option>
<option value="col4">5</option>
<option value="col5">6</option>
<option value="col6">7</option>
<option value="col7">8</option>
<option value="col8">9</option>
</select>
<INPUT TYPE="SUBMIT" NAME="" VALUE="submit"></form>

<?php


echo("$body[color]");
echo("$div[color]");


?>

firepages
04-19-2004, 04:40 AM
<?
echo $_REQUEST['body']['color']; //or $_GET
?>

oztinks
04-19-2004, 05:28 AM
Thanks,
but using either request or get still output the same value ie col1 not the hex code from the hidden fields.

this returns the correct value

<?




if ( $_GET['body']['color'] == 'col0' ) {
echo $_GET['col0'];
}

?>
But with around 100 fields using the hidden color fields that would be ugly code :eek:

Any other sugestions?

Garadon
04-19-2004, 06:31 AM
echo $_GET[$_GET['body']['color']];


just 1 comment unless you plan on having multiple colors for body u really don't need that body array.

firepages
04-19-2004, 06:49 AM
But with around 100 fields using the hidden color fields that would be ugly code :eek:Any other sugestions?


<option value="#FFFFFF">1</option>

;)

oztinks
04-19-2004, 07:53 AM
<option value="#FFFFFF">1</option>
Life would be simpler but so much more boring :)

Thanks Garadon that has done the trick but.... why is there always a but?
The body array has other values some using the hidden fields others from normal user input and I was going to use foreach to display all the values but now those values are empty

this is the return from this form with bold selected for font-weight

color : #7F7F7F ;
background-color : #A6A6A6 ;
font-weight : ;

a bigger bit of the form looks like this


<FORM ACTION="" METHOD="POST" TARGET="<?php $PHP_SELF ?>" >
<input type="hidden" name="col0" id="col0" value="#808080" />
<input type="hidden" name="col1" id="col1" value="#A6A6A6" />
<input type="hidden" name="col2" id="col2" value="#CCCCCC" />
<input type="hidden" name="col3" id="col3" value="#7F7F7F" />
<input type="hidden" name="col4" id="col4" value="#A6A6A6" />
<input type="hidden" name="col5" id="col5" value="#CCCCCC" />
<input type="hidden" name="col6" id="col6" value="#7F7F7F" />
<input type="hidden" name="col7" id="col7" value="#7F7F7F" />
<input type="hidden" name="col8" id="col8" value="#000000" />
<select name="body[color]">
<option value="">None</option>
<option value="col0">1</option>
<option value="col1">2</option>
<option value="col2">3</option>
<option value="col3">4</option>
<option value="col4">5</option>
<option value="col5">6</option>
<option value="col6">7</option>
<option value="col7">8</option>
<option value="col8">9</option>
</select><select name="body[background-color]">
<option value="">None</option>
<option value="col0">1</option>
<option value="col1">2</option>
<option value="col2">3</option>
<option value="col3">4</option>
<option value="col4">5</option>
<option value="col5">6</option>
<option value="col6">7</option>
<option value="col7">8</option>
<option value="col8">9</option>
</select>

<select name="body[font-weight]'">
<option value="">Normal</option>
<option value="bold">Bold</option>
</select>

<INPUT TYPE="SUBMIT" NAME="" VALUE="submit"></form>

<?php

$p = &$_POST;
$body = '';
foreach($p['body'] as $key => $value) {
$body .= $key . ' : ' . $p[$value] . " ;<br />";

}

echo $body ;
?>


Am I going about this the wrong way?

firepages
04-19-2004, 08:13 AM
Life would be simpler but so much more boring :)

sorry I should be more verbose ... I mean



<?
$cols =array('#808080','#A6A6A6','#CCCCCC','#7F7F7F','#A6A6A6','#CCCCCC','#7F7F7F','#7F7F7F','#000000');
?>
<select name="body[color]">
<option value="">None</option>
<?foreach($cols as $k=>$v){?>
<option value="<?=$v;?>">col<?=$k.' ('.$v.')';?></option>
<?}?>
</select>

oztinks
04-20-2004, 06:01 AM
sorry I should be more verbose


No need to apologise, I am finding php pretty confusing but I got a good laugh imagining my form with a single color option "white"
:D

Thanks for the code I have changed my hidden elements to an array and it works fine when I drop the code into my page, but I have a function to write the color selection and I cant get that to work I guess it is about making my array global but after 5 hours I am still no closer to figuring out how to make it work :confused:

the broken function is

function colorselect($tag)
{
echo '<select name="' . $tag . '">
<option value="">None</option>';
foreach($cols as $k=>$v){
echo '<option value="'.$v.'">col'.$k.' ('.$v.')</option> </select>';

}
}

oztinks
04-20-2004, 06:21 AM
DUHHHH... :o



I guess it is about making my array global
Guess I knew what to do all along

global $cols;

Now it works :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum