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.
It's very simple.
The checkbox field is like an input text. If you don't write anything in the field, it will say the field doesn't exist.
I think the value for the
$_POST['color']
should be read only after checking if its set.What i suggest is , its better to use post than get. here are some difference between post VS get
Some notes on GET requests:
Some notes on POST requests:
HTML Code
PHP code
If you want to turn specific values in to new variables if they have been selected:
foreach is the best way to get array of values.
here the example code: html code:
phpcode: