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.
If you need to submit a form when a checkbox is checked or when it is unchecked like when you are using a switch, a good way is to create an hidden input. If you try to submit the checkbox argument if the checkbox is unchecked the form will not be submitted at all.
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
<form action="third.php" method="get"> Red<input type="checkbox" name="color[]" value="red"> Green<input type="checkbox" name="color[]" value="green"> Blue<input type="checkbox" name="color[]" value="blue"> Cyan<input type="checkbox" name="color[]" value="cyan"> Magenta<input type="checkbox" name="color[]" value="Magenta"> Yellow<input type="checkbox" name="color[]" value="yellow"> Black<input type="checkbox" name="color[]" value="black"> <input type="submit" value="submit"> </form>
###PHP (using $_GET) using third.php
as your handler
<?php $name = $_GET['color']; // optional // echo "You chose the following color(s): <br>"; foreach ($name as $color){ echo $color."<br />"; } ?>
Assuming 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.
<?php $name = $_GET['color']; if (isset($_GET['color'])) { echo "You chose the following color(s): <br>"; foreach ($name as $color){ echo $color."<br />"; } } else { echo "You did not choose a color."; } ?>
##Additional options: To appear as a list: (<ul></ul>
can be replaced by <ol></ol>
)
<?php $name = $_GET['color']; if (isset($_GET['color'])) { echo "You chose the following color(s): <br>"; echo "<ul>"; foreach ($name as $color){ echo "<li>" .$color."</li>"; } echo "</ul>"; } else { echo "You did not choose a color."; } ?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With