Re: Radio Buttons



J.O. Aho wrote:
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>

Hi!

I must have something else wrong in my code; php probably. I tried your code and still didn't get the php file to work. Someone told me that my "id" and "name" in the properties box had to be the same. One of the examples in the book has them with different names. Will that make a difference?

.