Discussion:
radio buttons in $_POST
Ashley M. Kirchner
2005-04-23 19:22:49 UTC
Permalink
I have this form.html:


<form method="post" action="form.php">
text input <input type="text" name="input_test"><br />
radio input1 <input type="radio" name="radio_test" value="test1" />
radio input2 <input type="radio" name="radio_test" value="test2" />
<input type="submit" value="Submit">
</form>


Then I have this script (form.php):

<?php
foreach ($_POST as $key => $val)
echo "$key = $val\n";
?>


If I simply submit the form empty, all I get back from the script is
the text input $key. The radio button doesn't show up. Why is that and
how can I have it show up? (the radio input does show up once one of
the radios is checked, but I need it to show up when empty as well.)
--
H | I haven't lost my mind; it's backed up on tape somewhere.
+--------------------------------------------------------------------
Ashley M. Kirchner <mailto:***@pcraft.com> . 303.442.6410 x130
IT Director / SysAdmin / WebSmith . 800.441.3873 x130
Photo Craft Imaging . 3550 Arapahoe Ave. #6
http://www.pcraft.com ..... . . . Boulder, CO 80303, U.S.A.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Hegedűs Balázs
2005-04-23 19:42:25 UTC
Permalink
Hi,

You solve the problem yourself: "the radio input does show up once one
of the radios is checked". When it is unchecked is isn't set...if you
need to assign it a value in any circumstances you should test it with
<?php $chkbox1 = (isset($_POST["radio_test"])) ? true : false; ?> for
example (of course you may omit the ternary operator...isset() will do
the same).
Post by Ashley M. Kirchner
<form method="post" action="form.php">
text input <input type="text" name="input_test"><br />
radio input1 <input type="radio" name="radio_test" value="test1" />
radio input2 <input type="radio" name="radio_test" value="test2" />
<input type="submit" value="Submit">
</form>
<?php
foreach ($_POST as $key => $val)
echo "$key = $val\n";
?>
If I simply submit the form empty, all I get back from the script
is the text input $key. The radio button doesn't show up. Why is
that and how can I have it show up? (the radio input does show up
once one of the radios is checked, but I need it to show up when empty
as well.)
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
M Saleh EG
2005-04-23 19:43:08 UTC
Permalink
That's because Radio Buttons and Check Boxes do not return anything if not
checked or selected.
So you gotta explicitly do it ur self. That is u gotta check for it's
validity and non-empiness.
e.g. in ur scenario
if(!isset($_POST['radio_test'])) { //print out all the options unselected }
else
{ // print the value and show the rest empty in the report }
Post by Ashley M. Kirchner
<form method="post" action="form.php">
text input <input type="text" name="input_test"><br />
radio input1 <input type="radio" name="radio_test" value="test1" />
radio input2 <input type="radio" name="radio_test" value="test2" />
<input type="submit" value="Submit">
</form>
<?php
foreach ($_POST as $key => $val)
echo "$key = $val\n";
?>
If I simply submit the form empty, all I get back from the script is
the text input $key. The radio button doesn't show up. Why is that and
how can I have it show up? (the radio input does show up once one of
the radios is checked, but I need it to show up when empty as well.)
--
H | I haven't lost my mind; it's backed up on tape somewhere.
+--------------------------------------------------------------------
IT Director / SysAdmin / WebSmith . 800.441.3873 x130
Photo Craft Imaging . 3550 Arapahoe Ave. #6
http://www.pcraft.com ..... . . . Boulder, CO 80303, U.S.A.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
M.Saleh.E.G
97150-4779817
Janet Valade
2005-04-23 19:56:05 UTC
Permalink
Post by Ashley M. Kirchner
<form method="post" action="form.php">
text input <input type="text" name="input_test"><br />
radio input1 <input type="radio" name="radio_test" value="test1" />
radio input2 <input type="radio" name="radio_test" value="test2" />
<input type="submit" value="Submit">
</form>
<?php
foreach ($_POST as $key => $val)
echo "$key = $val\n";
?>
If I simply submit the form empty, all I get back from the script is
the text input $key. The radio button doesn't show up. Why is that and
how can I have it show up? (the radio input does show up once one of
the radios is checked, but I need it to show up when empty as well.)
One way would be to have a selection "None" and have it selected by
default. As in,

None <input type="radio" checked name="radio_test" value="None" />

Or you can just do something like,

<?php
if(!isset[$_POST['radio_test']))
echo "radio_test = none\n";
foreach ($_POST as $key => $val)
echo "$key = $val\n";
?>


Janet
--
Janet Valade -- janet.valade.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Justin Gruenberg
2005-04-23 20:25:07 UTC
Permalink
Post by Janet Valade
None <input type="radio" checked name="radio_test" value="None" />
And if you didn't want it to show to the user (because they have to
make a choice anyway) . . . you can use this css:

input[value="None"]
{
display: none;
}

You should then always have a value to play with in PHP, and the user
doesn't see any extra clutter.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
M. Sokolewicz
2005-04-23 22:15:08 UTC
Permalink
Post by Justin Gruenberg
Post by Janet Valade
None <input type="radio" checked name="radio_test" value="None" />
And if you didn't want it to show to the user (because they have to
input[value="None"]
{
display: none;
}
You should then always have a value to play with in PHP, and the user
doesn't see any extra clutter.
which unfortunately currently only works in a couple of browsers
(mozilla, (and thus firefox), and opera I think (?))
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Continue reading on narkive:
Loading...