/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Form, hidden field, passing variable

Again, another problem. On the first page I need to create a random hex color (no problems there) and put it into a form. When the user clicks the Submit button, they get taken to a page that has that random hex color from the previous page as the second/result page’s background color. The problem I’m having is getting the background color to come up. I’m not sure if I’m passing the variable correctly or something. Our teacher is nuts, he thinks we’ve learned this. To pass the variable, I’m using a hidden input field.

First page code:

[code=php]<?xml version=”1.0″ encoding=”utf-8″?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en”>
<head>
<title>
2910_27.php
</title>
</head>
<body>
<!–Code ommited–>
<?php
$y=1;
while ($y <= 6)
{
$x=rand(0,1);
if ($x == 0)
{
$z=rand(0,9);
}
else
{
$z=chr(rand(65,90));
}
$y++;
$w=$w.$z;
}
?>
<form method=”post” action=”2910_27_get.php” name=”form”><br />
Background color:<input type=”hidden” name=”hex” value=”<?php echo $w;?>” /> #
<?php
echo $w.”;”;
?>
<br />
<input type=”submit” value=”Make the Background Color” />
</body>
</html>[/code]

Results page:

[code=php]<?xml version=”1.0″ encoding=”utf-8″?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en”>
<head>
<title>
2910_27_get.php
</title>
<style type=”text/css”>
body
{
background-color:#
<?php
$w=$_POST[‘hex’];
echo $w.”;”;
?>
}
</style>
</head>
<body>
<!–code ommited–>
Happy Day!
</body>
</html>[/code]

When the results page comes up, the only output is “Happy Day!”. I’m not sure if I goofed the code and forgot a comma or period somewhere, but my classmates and I can’t find any error like that. Which makes me think I’m messing up the actual action of passing the variable.

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@aj_nscFeb 24.2010 — Everything looks good, could be your css though. Right now it outputs:
<i>
</i>background-color: #
FFFFFF;

(ffffff is an example hex code)

Keep it all on the same line:

[code=php]
background-color: #<?php echo $_POST['hex']; ?>;
[/code]


As you can see, there's also no need to create an unnecessary intermediate variable $w
Copy linkTweet thisAlerts:
@rainstinksauthorFeb 24.2010 — I changed the line to [code=php]background-color:#<?php echo $_POST['hex']; ?>;[/code] like you suggested, but I'm still not getting anything except "Good Day!" on the results page. Like you said, I'm thinking it has to do with the CSS part, but I'm not sure exactly what's wrong.

Revised results page code:
[code=php]<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>
2910_27_get.php
</title>
<style type="text/css">
body
{
background-color:#<?php echo $_POST['hex']; ?>;
}
</style>
</head>
<body>
<!--author:shane nelson-->
Happy Day!
</body>
</html>[/code]
Copy linkTweet thisAlerts:
@MindzaiFeb 24.2010 — Your code is probably generating invalid hex numbers most of the time. The only valid letters in base 16 are A-F (which represent 10-16). You are generating A-Z.

Also remember the point about reassigning values to new variables - it's messy and unnecessary - and you are also forgetting to clean the supplied data before displaying it.
Copy linkTweet thisAlerts:
@SrWebDeveloperFeb 24.2010 — @OP

This code tested/works, notice the code I used to generate the random hex based on random RGB values (each can be 0-255 decimal), a different approach but if you think about it CSS really works this way. In the result page I basically did what you did as well, only difference is if the script is called directly and not via form submission it'll default to a white background.

[code=php]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>
2910_27.php
</title>
</head>
<body>
<!--Code ommited-->
<?
$w=sprintf("&#37;02X%02X%02X", mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));

?>
<form method="post" action="2910_27_get.php" name="form"><br />
Background color:<input type="hidden" name="hex" value="<? echo $w; ?>" /> #
<? echo $w.';'; ?>
<br />
<input type="submit" value="Make the Background Color" />
</form>
</body>
</html>[/code]



[code=php]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>
2910_27_get.php
</title>
<style type="text/css">
body
{
background-color: <?php print (isset($_POST['hex'])) ? "#{$_POST['hex']}" : "white"; ?>;
}
</style>
</head>
<body>
<!--code ommited-->
Happy Day!
</body>
</html>[/code]
Copy linkTweet thisAlerts:
@rainstinksauthorFeb 25.2010 — Wow, I feel so stupid. I thought I had typed "$rand=chr(rand(65,70))" to make it stop at F. Thanks for pointing that out.

And SrWebDeveloper, what you just did is way past what I can do right now. That made no sense to me, heh. But I got it to work thanks to Mindzai finding that "chr(rand(65,90))" mistake I made.
Copy linkTweet thisAlerts:
@SrWebDeveloperFeb 25.2010 — We all do stuff like that, don't worry. Been there, done that.

I'll explain the HEX algorithm I used so you can move to the next level in your brain's capacity for using PHP, so to speak! :p

[code=php]$w=sprintf("&#37;02X%02X%02X", mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255))[/code]

Okay, what that does is using sprintf, a cool PHP function that allows you to format a string. Notice there are 3 instances of %2X in sequence. They each tell sprintf to output a 2 digit (%2) upper case hexadecimal number (X) - how convenient is that! The 3 instances of mt_rand(0,255) are used to generate 3 random integers each between 0 and 255 which seeds each instance of %2X, in order, just like how RGB works. mt_rand is faster and more random than rand.

So this uses some nifty PHP functions in combination to get the job done easily and with less code.

Now you know! Just file it away in your brain for later use.

-jim
Copy linkTweet thisAlerts:
@rainstinksauthorMar 01.2010 — Wow, that is definitely quicker and better than rand. I read up on it at PHP.net. So, is it basically the same as rand() when using it in the code?
×

Success!

Help @rainstinks spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.14,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...