/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] variable–>result without a form

I’ve been having some trouble with another example in my programming class, and I know how to do it…but I don’t know how to [i]execute[/i] it.

What we have to do is generate a random character (A-Z) and make it a hyperlink going to another page saying which letter was clicked. What I can’t figure out (though I have a theory) is how to pass the variable that is the random character to the next page. I was thinking of trying to pass it through the URL, but that is [i]way[/i] past what I’ve learned, and I think it’d be above my head. The other way I was thinking about was having a form, but I don’t understand how to create a form field that wouldn’t be shown but would still have the variable.

Here’s my first page’s code, if it is needed:

[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_25.php
</title>
</head>
<body>
<!–Code excluded(name)–>
<?php
$y=1;
while ($y <= 50)
{
$x=chr(rand(65,90));
echo “<a href=’http://ito.auburncc.org/localuser/snelson/month_2_of_10/2910_25_get.php’>”.$x.”</a>”;
$y++;
}
?>
</body>
</html>[/code]

Any help would be greatly appreciated.

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiFeb 24.2010 — Have a look at this page (it is from a javascript tutorial but the specific page I linked is relevant for PHP too).

Then have a look at this page, paying particular attention to the example.
Copy linkTweet thisAlerts:
@rainstinksauthorFeb 24.2010 — Thanks a lot Mindzai. That was a lot simpler than I thought it was going to be. Those pages/examples made it make sense a lot quicker than what I had been looking at before.

If anyone needs to see the code I have for the pages for future reference, here it is:

First 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_25.php
</title>
</head>
<body>
<!--code excluded (name)-->
<?php
$y=1;
while ($y <= 50)
{
$x=chr(rand(65,90));
echo "<a href='2910_25_get.php?letter=".$x."'>".$x."</a><br />";
$y++;
}
?>
</body>
</html>[/code]


And Result:
[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_25_get.php
</title>
</head>
<body>
<!--author:Shane Nelson-->
<?php
$y=$_GET['letter'];
echo "You clicked ".$y."!";
?>
</body>
</html>[/code]
Copy linkTweet thisAlerts:
@MindzaiFeb 24.2010 — Don't forget that for security reasons any input received by a script like this needs to be cleaned before it is displayed to the user. For this reason, you should use htmlspecialchars on the value before displaying it. If you don't do this, you leave yourself open to cross site scripting (XSS) attacks.

Also note that there is no reason to reassign variables from $_GET - doing so just introduces complexity and therefore the potential for errors.

The following amendment to your code addresses the two issues above:

[code=php]
<?php
echo "You clicked " . htmlspecialchars($_GET['letter']) . "!";
?>
[/code]
Copy linkTweet thisAlerts:
@rainstinksauthorFeb 24.2010 — Alright, I'll change it. I didn't even realize anything like that could happen. Thanks a lot!
×

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.17,
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,
)...