I have 6 options, I want to get the checked values to store them in variable on second page. How do I go on doing that?
<form action="third.php" method="get">
<!-- Choices -->
Red <input type="checkbox" name="color[]" id="color" value="Red">
Green <input type="checkbox" name="color[]" id="color" value="Green">
Blue <input type="checkbox" name="color[]" id="color" value="Blue">
Cyan <input type="checkbox" name="color[]" id="color" value="Cyan">
Magenta <input type="checkbox" name="color[]" id="color" value="Magenta">
Yellow <input type="checkbox" name="color[]" id="color" value="Yellow">
Black <input type="checkbox" name="color[]" id="color" value="Black">
<!-- Submit -->
<input type="submit" value="submit">
</form>
And third.php
page :
$color = $_GET['color'];
echo 'The color is '.$color;
If I remove []
, I get the color is on, when I do it like color[]
I get a notice saying :
Array to string conversion
What I want is the value of checked, checkboxes so I can store it in variable.
Just for printing you can use as below :
or
A good method which is a favorite of mine and for many I'm sure, is to make use of
foreach
which will output each color you chose, and appear on screen one underneath each other.When it comes to using checkboxes, you kind of do not have a choice but to use
foreach
, and that's why you only get one value returned from your array.Here is an example using
$_GET
. You can however use$_POST
and would need to make both directives match in both files in order to work properly.HTML FORM
PHP (using $_GET) using
third.php
as your handlerAssuming having chosen red, green, blue and cyan as colors, will appear like this:
red
green
blue
cyan
OPTION #2
You can also check if a color was chosen. If none are chosen, then a seperate message will appear.
Additional options:
To appear as a list: (
<ul></ul>
can be replaced by<ol></ol>
)Flag: (August the 5th, 2016)
I've received 2 downvotes this morning and my comments we're removed; why? Whoever's doing this, is obvioulsy doing this with malice. The answer has received nothing but upvotes and now this. I mean seriously; wtf? – Fred -ii- 1 hour ago declined - People are free to vote how they want, as long as you're not being targeted. I see no evidence of that. Your comments about votes were noise, flagged as such, and removed.
Answer to this: That's just the thing; I am.
(It's not
action="get"
oraction="post"
it'smethod="get"
ormethod="post"
Try to do it using post method:
and in third.php
or for a pericular field you colud get value in:
Perhaps a better way is using the php function in_array() like this:
To start with we have created a variable
$style
to set if we want the options in a horizontal or vertical way. This will infrequence how we display our checkboxes. Next we set the$name
for our options, this is needed as a name of the array where we want to keep our options. I have created a loop here to construct each option as given in the array$options
, then we check each item if it should be checked in our returned form. I believe this should simplify the way we can reproduce a form with checkboxes.