You are here: Re: Radio Buttons « All PHP « IT news, forums, messages
Re: Radio Buttons

Posted by J.O. Aho on 04/16/07 18:54

DrNoose wrote:
> Hi!
>
> I have followed the previous threads and tried some of the suggestions,
> but still could not get my code to run. When I select a button and hit
> the "Pick A Color" button, nothing happens. I've included the code for
> the html form and the .php file. The book I've been reading suggests
> using If/else statements. I saw that some else had used switch
> statements. I did try using a switch statement, but it didn't work. The
> code below uses the if/else statements. I was also not sure if I should
> put the $_POST statement in the if/else since I had used "extract
> "$_POST" earlier.
>
> Any help is appreciated!
>
> **************************************************************************
>
> <!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" >
> <head>
> <title>Pick A Color</title>
> </head>
> <body>
> <form method="post" action="PickAColor.php">
> <p style="text-align: center">
> <strong><span style="font-size: 24pt">Pick A
> Color</span></strong></p>
> <p>
> &nbsp;</p>
> <p>
> <strong><span style="font-size: 16pt" title="Red">
> <input id="btnRed" name="PickColor" type="radio"
> value="Red" />Red</span></strong></p>
> <p>
> <strong><span style="font-size: 16pt" title="Red">
> <input id="btnBlue" name="PickColor" tabindex="1"
> type="radio" value="Blue" />Blue</span></strong></p>
> <p>
> <strong><span style="font-size: 16pt" title="Red">
> <input id="btnGreen" name="PickColor" tabindex="2"
> type="radio" value="Green" />Green</span></strong></p>
> <p>
> <input id="btnPickColor" name="PickColor" type="button"
> value="Pick A Color" />&nbsp;</p>
> <p>
> <strong><span style="font-size: 16pt"
> title="Red"></span></strong>&nbsp;</p>
>
> </form>
> </body>
> </html>
>
> <?php
> // Get the information from the form
> extract($_POST);
> // Determine if the data is blank
> if ($_POST[PickAColor == "")

Not strange it don't work, as using $_POST[PickAColor] requires a Enum called
PickAColor that will have the same value as the celld $_POST['PickAColor']
has. And secondly you don't send any PickAColor, but only PickColor.

> {
> header("Location: index.html");
> exit;
> }
>
> // Determine what index was selected
> if ($_POST[PickColor] == "Red") {
> $resultIndex = 0;
>
> } else if ($_POST[PickColor] == "Blue") {
> $resultIndex = 1;
>
> } else if ($_POST[PickColor] == "Green") {
> $resultIndex = 2;
> }
>
> // Determine what color was selected
> if ($_POST[PickColor] == "Red") {
> $resultColor = Red;
>
> } else if ($_POST[PickColor] == "Blue") {
> $resultColor = Blue;
>
> } else if ($_POST[PickColor] == "Green") {
> $resultColor = Green;
> }
> ?>
> <html>
> <head>
> <title>Pick A Color</title>
> </head>
> <body>
> <p> You selected Index Number: <?php echo $resultIndex"; ?></p>
> <p> The color you selecte is: <?php echo "$resultColor"; ?></p>>
>
> </body>
> </html>

Try this one instead

<?PHP
/* If PickAColor wasn't used redirect them to main page */
if (!isset($_POST['PickColor']) || empty($_POST['PickAColor'])) {
header("Location: index.html");
exit;
}
/* Here we check what value was selected and set the two
varibles resultIndex and resultColor */
switch($_POST['PickColor']) {
case 'Red':
$resultIndex = 0;
$resultColor = "Red";
break;
case 'Blue':
$resultIndex = 1;
$resultColor = "Blue";
break;
case 'Green':
$resultIndex = 2;
$resultColor = "Green";
break;
default:
/* Some one has tried to send false values */
header("Location: index.html");
exit;
break;
}
?>
<html>
<head>
<title>Pick A Color</title>
</head>
<body>
<p> You selected Index Number: <?php echo $resultIndex"; ?></p>
<p> The color you selecte is: <?php echo "$resultColor"; ?></p>
</body>
</html>

--

//Aho

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация