Getting checkbox values on submit
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.
php forms
|
show 1 more comment
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.
php forms
1
@blo i edited it.
– Nofel
Aug 24 '13 at 19:21
1
@MadaraUchiha thank you, i edited the question.
– Nofel
Aug 24 '13 at 19:22
2
Its not action="get" its method="get" !
– Rahul
Aug 24 '13 at 19:48
1
@AmmarKhan Have a look at my answer below, with a few additional options you can use.
– Funk Forty Niner
Aug 24 '13 at 23:59
1
I would suggest to usepost
method instead ofget
for form submit.
– Naina
Mar 17 '14 at 8:10
|
show 1 more comment
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.
php forms
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.
php forms
php forms
edited Oct 1 '14 at 10:02
Nofel
asked Aug 24 '13 at 18:53
NofelNofel
5361830
5361830
1
@blo i edited it.
– Nofel
Aug 24 '13 at 19:21
1
@MadaraUchiha thank you, i edited the question.
– Nofel
Aug 24 '13 at 19:22
2
Its not action="get" its method="get" !
– Rahul
Aug 24 '13 at 19:48
1
@AmmarKhan Have a look at my answer below, with a few additional options you can use.
– Funk Forty Niner
Aug 24 '13 at 23:59
1
I would suggest to usepost
method instead ofget
for form submit.
– Naina
Mar 17 '14 at 8:10
|
show 1 more comment
1
@blo i edited it.
– Nofel
Aug 24 '13 at 19:21
1
@MadaraUchiha thank you, i edited the question.
– Nofel
Aug 24 '13 at 19:22
2
Its not action="get" its method="get" !
– Rahul
Aug 24 '13 at 19:48
1
@AmmarKhan Have a look at my answer below, with a few additional options you can use.
– Funk Forty Niner
Aug 24 '13 at 23:59
1
I would suggest to usepost
method instead ofget
for form submit.
– Naina
Mar 17 '14 at 8:10
1
1
@blo i edited it.
– Nofel
Aug 24 '13 at 19:21
@blo i edited it.
– Nofel
Aug 24 '13 at 19:21
1
1
@MadaraUchiha thank you, i edited the question.
– Nofel
Aug 24 '13 at 19:22
@MadaraUchiha thank you, i edited the question.
– Nofel
Aug 24 '13 at 19:22
2
2
Its not action="get" its method="get" !
– Rahul
Aug 24 '13 at 19:48
Its not action="get" its method="get" !
– Rahul
Aug 24 '13 at 19:48
1
1
@AmmarKhan Have a look at my answer below, with a few additional options you can use.
– Funk Forty Niner
Aug 24 '13 at 23:59
@AmmarKhan Have a look at my answer below, with a few additional options you can use.
– Funk Forty Niner
Aug 24 '13 at 23:59
1
1
I would suggest to use
post
method instead of get
for form submit.– Naina
Mar 17 '14 at 8:10
I would suggest to use
post
method instead of get
for form submit.– Naina
Mar 17 '14 at 8:10
|
show 1 more comment
10 Answers
10
active
oldest
votes
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" 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">
<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.";
}
?>
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.
5
whoever just dv this, also dv another of my answers stackoverflow.com/a/18382062/1415724 obviously someone coming from answers in this question stackoverflow.com/q/43875382/1415724 I am sure of it. If they can't take the downvote for what should have been a comment, then they shouldn't post at all; IMHO. Downvote me if you want, I don't care, but "you" lose points in doing that, remember that.
– Funk Forty Niner
May 9 '17 at 16:54
add a comment |
(It's not action="get"
or action="post"
it's method="get"
or method="post"
Try to do it using post method:
<form action="third.php" method="POST">
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">
<input type="submit" value="submit">
</form>
and in third.php
or for a pericular field you colud get value in:
$_POST['color'][0] //for RED
$_POST['color'][1] // for GREEN
Ahh, so this is when things start to get wonky with PHP. $_POST is an array, and when looking at the solutions, I wondered, "$_POST['color'] is also it's own array, too, isn't it?"
– DeltaFlyer
Nov 30 '18 at 20:07
add a comment |
What i suggest is , its better to use post than get. here are some difference between post VS get
Some notes on GET requests:
- GET requests can be cached
- GET requests remain in the browser history
- GET requests can be bookmarked
- GET requests should never be used when dealing with sensitive data
- GET requests have length restrictions
- GET requests should be used only to retrieve data
Some notes on POST requests:
- POST requests are never cached
- POST requests do not remain in the browser history
- POST requests cannot be bookmarked
- POST requests have no restrictions on data length
HTML Code
<html>
<head></head>
<body>
<form action="output.php" method="post">
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">
<input type="submit" value="submit">
</form>
<body>
</html>
PHP code
<?php
if(isset($_POST['color'])) {
$name = $_POST['color'];
echo "You chose the following color(s): <br>";
foreach ($name as $color){
echo $color."<br />";
}} // end brace for if(isset
else {
echo "You did not choose a color.";
}
?>
What's about 2-dimension arrays? Does GET support something like this:<input type="checkbox" name="color[categoryA]" id="color" value="red"> <input type="checkbox" name="color[categoryA]" id="color" value="green">
which will work with mehtod=POST
– The Bndr
Apr 24 '18 at 11:56
add a comment |
Just for printing you can use as below :
print_r($_GET['color']);
or
var_dump($_GET['color']);
add a comment |
I think the value for the $_POST['color']
should be read only after checking if its set.
<?php
if(isset($_POST['color'])) {
$name = $_POST['color'];
echo "You chose the following color(s): <br>";
foreach ($name as $color){
echo $color."<br />";
}} // end brace for if(isset
else {
echo "You did not choose a color.";
}
?>
add a comment |
Perhaps a better way is using the php function in_array() like this:
$style='V';//can be 'V'ertical or 'H'orizontal
$lineBreak=($style=='V')?'<br>':'';
$name='colors';//the name of your options
$Legent="Select your $name";//dress it up in a nice fieldset with a ledgent
$options=array('red','green','blue','orange','yellow','white','black');
$boxes='';//innitiate the list of tickboxes to be generated
if(isset($_REQUEST["$name"])){
//we shall use $_REQUEST but $_POST would be better
$Checked=$_REQUEST["$name"];
}else{
$Checked=array();
}
foreach($options as $option){
$checkmark=(in_array($option,$Checked))?'checked':'';
$nameAsArray=$name.'';//we would like the returned data to be in an array so we end with
$boxes.=($style=='V')?"<span class='label2' align='right'><b>$option : </b></span>":"<b>$option </b>";
$boxes.="<input style='width:2em;' type='checkbox' name='$nameAsArray' id='$option' value='$option' $checkmark >$lineBreak";
}
echo<<<EOF
<html>
<head></head>
<body>
<form name="Update" method="GET" action="{$_SERVER['PHP_SELF']}">n
<fieldset id="tickboxes" style="width:25em;">
<legend>{$Legent}</legend>
{$boxes}
</fieldset>
<button type="submit" >Submit Form</button>
</form>
<body>
</html>
EOF
;
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.
add a comment |
If you want to turn specific values in to new variables if they have been selected:
// Retrieve array color and set as variable
$colors = $_GET['color'];
// Use array_search to find the key for "red"
$key_red = array_search('red', $colors);
// If "red" exists, the key will be an integer (or FALSE)
if (is_int($key_red)) {
$red_color = 'Red was selected';
}
add a comment |
foreach is the best way to get array of values.
here the example code:
html code:
<form action="send.php" method="post">
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">
<input type="submit" value="submit">
</form>
phpcode:
<?php
$name = $POST['color'];
foreach ($name as $color){
echo $color."<br />";
}
?>
cool answer !! it works
– anonymouse
Jun 19 '18 at 5:34
add a comment |
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.
<form method="post">
<input type="checkbox" name="check">This is how it works!<br>
<button type="submit" name="submit">Submit</button>
</form>
<?php
if(isset($_POST['submit'])) {
if(!isset($_POST['check'])) {
echo "Not selected!";
}else{
echo "Selected!";
}
}
?>
add a comment |
//retrive check box and gender value
$gender=$row['gender'];
$chkhobby=$row['chkhobby'];
<tr>
<th>GENDER</th>
<td>
Male<input type="radio" name="gender" value="1" <?php echo ($gender== '1') ? "checked" : "" ; ?>/>
Female<input type="radio" name="gender" value="0" <?php echo ($gender== '0') ? "checked" : "" ; ?>/>
</td>
</tr>
<tr>
<th>Hobbies</th>
<td>
<pre><?php //print_r($row);
$hby = @explode(",",$row['chkhobby']);
//print_r($hby);
?></pre>
read<input id="check_1" type="checkbox" name="chkhobby" value="read" <?php if(in_array("read",$hby)){?> checked="checked"<?php }?>>
write<input id="check_2" type="checkbox" name="chkhobby" value="write" <?php if(in_array("write",$hby)){?> checked="checked"<?php }?>>
play<input id="check_4" type="checkbox" name="chkhobby" value="play" <?php if(in_array("play",$hby)){?> checked="checked"<?php }?>>
</td>
</tr>
//update
$gender=$_POST['gender'];
$chkhobby = implode(',', $_POST['chkhobby']);
1
Please don't paste row code without explanation.
– Al Foиce ѫ
Aug 11 '16 at 9:11
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f18421988%2fgetting-checkbox-values-on-submit%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
10 Answers
10
active
oldest
votes
10 Answers
10
active
oldest
votes
active
oldest
votes
active
oldest
votes
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" 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">
<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.";
}
?>
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.
5
whoever just dv this, also dv another of my answers stackoverflow.com/a/18382062/1415724 obviously someone coming from answers in this question stackoverflow.com/q/43875382/1415724 I am sure of it. If they can't take the downvote for what should have been a comment, then they shouldn't post at all; IMHO. Downvote me if you want, I don't care, but "you" lose points in doing that, remember that.
– Funk Forty Niner
May 9 '17 at 16:54
add a comment |
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" 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">
<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.";
}
?>
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.
5
whoever just dv this, also dv another of my answers stackoverflow.com/a/18382062/1415724 obviously someone coming from answers in this question stackoverflow.com/q/43875382/1415724 I am sure of it. If they can't take the downvote for what should have been a comment, then they shouldn't post at all; IMHO. Downvote me if you want, I don't care, but "you" lose points in doing that, remember that.
– Funk Forty Niner
May 9 '17 at 16:54
add a comment |
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" 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">
<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.";
}
?>
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.
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" 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">
<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.";
}
?>
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.
edited Jul 13 '17 at 21:19
Tim Holt
2,61911219
2,61911219
answered Aug 24 '13 at 23:28
Funk Forty NinerFunk Forty Niner
1
1
5
whoever just dv this, also dv another of my answers stackoverflow.com/a/18382062/1415724 obviously someone coming from answers in this question stackoverflow.com/q/43875382/1415724 I am sure of it. If they can't take the downvote for what should have been a comment, then they shouldn't post at all; IMHO. Downvote me if you want, I don't care, but "you" lose points in doing that, remember that.
– Funk Forty Niner
May 9 '17 at 16:54
add a comment |
5
whoever just dv this, also dv another of my answers stackoverflow.com/a/18382062/1415724 obviously someone coming from answers in this question stackoverflow.com/q/43875382/1415724 I am sure of it. If they can't take the downvote for what should have been a comment, then they shouldn't post at all; IMHO. Downvote me if you want, I don't care, but "you" lose points in doing that, remember that.
– Funk Forty Niner
May 9 '17 at 16:54
5
5
whoever just dv this, also dv another of my answers stackoverflow.com/a/18382062/1415724 obviously someone coming from answers in this question stackoverflow.com/q/43875382/1415724 I am sure of it. If they can't take the downvote for what should have been a comment, then they shouldn't post at all; IMHO. Downvote me if you want, I don't care, but "you" lose points in doing that, remember that.
– Funk Forty Niner
May 9 '17 at 16:54
whoever just dv this, also dv another of my answers stackoverflow.com/a/18382062/1415724 obviously someone coming from answers in this question stackoverflow.com/q/43875382/1415724 I am sure of it. If they can't take the downvote for what should have been a comment, then they shouldn't post at all; IMHO. Downvote me if you want, I don't care, but "you" lose points in doing that, remember that.
– Funk Forty Niner
May 9 '17 at 16:54
add a comment |
(It's not action="get"
or action="post"
it's method="get"
or method="post"
Try to do it using post method:
<form action="third.php" method="POST">
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">
<input type="submit" value="submit">
</form>
and in third.php
or for a pericular field you colud get value in:
$_POST['color'][0] //for RED
$_POST['color'][1] // for GREEN
Ahh, so this is when things start to get wonky with PHP. $_POST is an array, and when looking at the solutions, I wondered, "$_POST['color'] is also it's own array, too, isn't it?"
– DeltaFlyer
Nov 30 '18 at 20:07
add a comment |
(It's not action="get"
or action="post"
it's method="get"
or method="post"
Try to do it using post method:
<form action="third.php" method="POST">
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">
<input type="submit" value="submit">
</form>
and in third.php
or for a pericular field you colud get value in:
$_POST['color'][0] //for RED
$_POST['color'][1] // for GREEN
Ahh, so this is when things start to get wonky with PHP. $_POST is an array, and when looking at the solutions, I wondered, "$_POST['color'] is also it's own array, too, isn't it?"
– DeltaFlyer
Nov 30 '18 at 20:07
add a comment |
(It's not action="get"
or action="post"
it's method="get"
or method="post"
Try to do it using post method:
<form action="third.php" method="POST">
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">
<input type="submit" value="submit">
</form>
and in third.php
or for a pericular field you colud get value in:
$_POST['color'][0] //for RED
$_POST['color'][1] // for GREEN
(It's not action="get"
or action="post"
it's method="get"
or method="post"
Try to do it using post method:
<form action="third.php" method="POST">
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">
<input type="submit" value="submit">
</form>
and in third.php
or for a pericular field you colud get value in:
$_POST['color'][0] //for RED
$_POST['color'][1] // for GREEN
edited Aug 24 '13 at 23:13
Funk Forty Niner
1
1
answered Aug 24 '13 at 19:47
RahulRahul
1,0231819
1,0231819
Ahh, so this is when things start to get wonky with PHP. $_POST is an array, and when looking at the solutions, I wondered, "$_POST['color'] is also it's own array, too, isn't it?"
– DeltaFlyer
Nov 30 '18 at 20:07
add a comment |
Ahh, so this is when things start to get wonky with PHP. $_POST is an array, and when looking at the solutions, I wondered, "$_POST['color'] is also it's own array, too, isn't it?"
– DeltaFlyer
Nov 30 '18 at 20:07
Ahh, so this is when things start to get wonky with PHP. $_POST is an array, and when looking at the solutions, I wondered, "$_POST['color'] is also it's own array, too, isn't it?"
– DeltaFlyer
Nov 30 '18 at 20:07
Ahh, so this is when things start to get wonky with PHP. $_POST is an array, and when looking at the solutions, I wondered, "$_POST['color'] is also it's own array, too, isn't it?"
– DeltaFlyer
Nov 30 '18 at 20:07
add a comment |
What i suggest is , its better to use post than get. here are some difference between post VS get
Some notes on GET requests:
- GET requests can be cached
- GET requests remain in the browser history
- GET requests can be bookmarked
- GET requests should never be used when dealing with sensitive data
- GET requests have length restrictions
- GET requests should be used only to retrieve data
Some notes on POST requests:
- POST requests are never cached
- POST requests do not remain in the browser history
- POST requests cannot be bookmarked
- POST requests have no restrictions on data length
HTML Code
<html>
<head></head>
<body>
<form action="output.php" method="post">
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">
<input type="submit" value="submit">
</form>
<body>
</html>
PHP code
<?php
if(isset($_POST['color'])) {
$name = $_POST['color'];
echo "You chose the following color(s): <br>";
foreach ($name as $color){
echo $color."<br />";
}} // end brace for if(isset
else {
echo "You did not choose a color.";
}
?>
What's about 2-dimension arrays? Does GET support something like this:<input type="checkbox" name="color[categoryA]" id="color" value="red"> <input type="checkbox" name="color[categoryA]" id="color" value="green">
which will work with mehtod=POST
– The Bndr
Apr 24 '18 at 11:56
add a comment |
What i suggest is , its better to use post than get. here are some difference between post VS get
Some notes on GET requests:
- GET requests can be cached
- GET requests remain in the browser history
- GET requests can be bookmarked
- GET requests should never be used when dealing with sensitive data
- GET requests have length restrictions
- GET requests should be used only to retrieve data
Some notes on POST requests:
- POST requests are never cached
- POST requests do not remain in the browser history
- POST requests cannot be bookmarked
- POST requests have no restrictions on data length
HTML Code
<html>
<head></head>
<body>
<form action="output.php" method="post">
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">
<input type="submit" value="submit">
</form>
<body>
</html>
PHP code
<?php
if(isset($_POST['color'])) {
$name = $_POST['color'];
echo "You chose the following color(s): <br>";
foreach ($name as $color){
echo $color."<br />";
}} // end brace for if(isset
else {
echo "You did not choose a color.";
}
?>
What's about 2-dimension arrays? Does GET support something like this:<input type="checkbox" name="color[categoryA]" id="color" value="red"> <input type="checkbox" name="color[categoryA]" id="color" value="green">
which will work with mehtod=POST
– The Bndr
Apr 24 '18 at 11:56
add a comment |
What i suggest is , its better to use post than get. here are some difference between post VS get
Some notes on GET requests:
- GET requests can be cached
- GET requests remain in the browser history
- GET requests can be bookmarked
- GET requests should never be used when dealing with sensitive data
- GET requests have length restrictions
- GET requests should be used only to retrieve data
Some notes on POST requests:
- POST requests are never cached
- POST requests do not remain in the browser history
- POST requests cannot be bookmarked
- POST requests have no restrictions on data length
HTML Code
<html>
<head></head>
<body>
<form action="output.php" method="post">
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">
<input type="submit" value="submit">
</form>
<body>
</html>
PHP code
<?php
if(isset($_POST['color'])) {
$name = $_POST['color'];
echo "You chose the following color(s): <br>";
foreach ($name as $color){
echo $color."<br />";
}} // end brace for if(isset
else {
echo "You did not choose a color.";
}
?>
What i suggest is , its better to use post than get. here are some difference between post VS get
Some notes on GET requests:
- GET requests can be cached
- GET requests remain in the browser history
- GET requests can be bookmarked
- GET requests should never be used when dealing with sensitive data
- GET requests have length restrictions
- GET requests should be used only to retrieve data
Some notes on POST requests:
- POST requests are never cached
- POST requests do not remain in the browser history
- POST requests cannot be bookmarked
- POST requests have no restrictions on data length
HTML Code
<html>
<head></head>
<body>
<form action="output.php" method="post">
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">
<input type="submit" value="submit">
</form>
<body>
</html>
PHP code
<?php
if(isset($_POST['color'])) {
$name = $_POST['color'];
echo "You chose the following color(s): <br>";
foreach ($name as $color){
echo $color."<br />";
}} // end brace for if(isset
else {
echo "You did not choose a color.";
}
?>
edited Dec 24 '15 at 18:25
Community♦
11
11
answered Jun 10 '14 at 4:52
Sathya BamanSathya Baman
1,46822351
1,46822351
What's about 2-dimension arrays? Does GET support something like this:<input type="checkbox" name="color[categoryA]" id="color" value="red"> <input type="checkbox" name="color[categoryA]" id="color" value="green">
which will work with mehtod=POST
– The Bndr
Apr 24 '18 at 11:56
add a comment |
What's about 2-dimension arrays? Does GET support something like this:<input type="checkbox" name="color[categoryA]" id="color" value="red"> <input type="checkbox" name="color[categoryA]" id="color" value="green">
which will work with mehtod=POST
– The Bndr
Apr 24 '18 at 11:56
What's about 2-dimension arrays? Does GET support something like this:
<input type="checkbox" name="color[categoryA]" id="color" value="red"> <input type="checkbox" name="color[categoryA]" id="color" value="green">
which will work with mehtod=POST– The Bndr
Apr 24 '18 at 11:56
What's about 2-dimension arrays? Does GET support something like this:
<input type="checkbox" name="color[categoryA]" id="color" value="red"> <input type="checkbox" name="color[categoryA]" id="color" value="green">
which will work with mehtod=POST– The Bndr
Apr 24 '18 at 11:56
add a comment |
Just for printing you can use as below :
print_r($_GET['color']);
or
var_dump($_GET['color']);
add a comment |
Just for printing you can use as below :
print_r($_GET['color']);
or
var_dump($_GET['color']);
add a comment |
Just for printing you can use as below :
print_r($_GET['color']);
or
var_dump($_GET['color']);
Just for printing you can use as below :
print_r($_GET['color']);
or
var_dump($_GET['color']);
answered Apr 6 '15 at 17:10
Vaibhavraj RohamVaibhavraj Roham
600417
600417
add a comment |
add a comment |
I think the value for the $_POST['color']
should be read only after checking if its set.
<?php
if(isset($_POST['color'])) {
$name = $_POST['color'];
echo "You chose the following color(s): <br>";
foreach ($name as $color){
echo $color."<br />";
}} // end brace for if(isset
else {
echo "You did not choose a color.";
}
?>
add a comment |
I think the value for the $_POST['color']
should be read only after checking if its set.
<?php
if(isset($_POST['color'])) {
$name = $_POST['color'];
echo "You chose the following color(s): <br>";
foreach ($name as $color){
echo $color."<br />";
}} // end brace for if(isset
else {
echo "You did not choose a color.";
}
?>
add a comment |
I think the value for the $_POST['color']
should be read only after checking if its set.
<?php
if(isset($_POST['color'])) {
$name = $_POST['color'];
echo "You chose the following color(s): <br>";
foreach ($name as $color){
echo $color."<br />";
}} // end brace for if(isset
else {
echo "You did not choose a color.";
}
?>
I think the value for the $_POST['color']
should be read only after checking if its set.
<?php
if(isset($_POST['color'])) {
$name = $_POST['color'];
echo "You chose the following color(s): <br>";
foreach ($name as $color){
echo $color."<br />";
}} // end brace for if(isset
else {
echo "You did not choose a color.";
}
?>
edited Dec 24 '15 at 17:39
answered Dec 24 '15 at 17:27
BlacboyBlacboy
32
32
add a comment |
add a comment |
Perhaps a better way is using the php function in_array() like this:
$style='V';//can be 'V'ertical or 'H'orizontal
$lineBreak=($style=='V')?'<br>':'';
$name='colors';//the name of your options
$Legent="Select your $name";//dress it up in a nice fieldset with a ledgent
$options=array('red','green','blue','orange','yellow','white','black');
$boxes='';//innitiate the list of tickboxes to be generated
if(isset($_REQUEST["$name"])){
//we shall use $_REQUEST but $_POST would be better
$Checked=$_REQUEST["$name"];
}else{
$Checked=array();
}
foreach($options as $option){
$checkmark=(in_array($option,$Checked))?'checked':'';
$nameAsArray=$name.'';//we would like the returned data to be in an array so we end with
$boxes.=($style=='V')?"<span class='label2' align='right'><b>$option : </b></span>":"<b>$option </b>";
$boxes.="<input style='width:2em;' type='checkbox' name='$nameAsArray' id='$option' value='$option' $checkmark >$lineBreak";
}
echo<<<EOF
<html>
<head></head>
<body>
<form name="Update" method="GET" action="{$_SERVER['PHP_SELF']}">n
<fieldset id="tickboxes" style="width:25em;">
<legend>{$Legent}</legend>
{$boxes}
</fieldset>
<button type="submit" >Submit Form</button>
</form>
<body>
</html>
EOF
;
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.
add a comment |
Perhaps a better way is using the php function in_array() like this:
$style='V';//can be 'V'ertical or 'H'orizontal
$lineBreak=($style=='V')?'<br>':'';
$name='colors';//the name of your options
$Legent="Select your $name";//dress it up in a nice fieldset with a ledgent
$options=array('red','green','blue','orange','yellow','white','black');
$boxes='';//innitiate the list of tickboxes to be generated
if(isset($_REQUEST["$name"])){
//we shall use $_REQUEST but $_POST would be better
$Checked=$_REQUEST["$name"];
}else{
$Checked=array();
}
foreach($options as $option){
$checkmark=(in_array($option,$Checked))?'checked':'';
$nameAsArray=$name.'';//we would like the returned data to be in an array so we end with
$boxes.=($style=='V')?"<span class='label2' align='right'><b>$option : </b></span>":"<b>$option </b>";
$boxes.="<input style='width:2em;' type='checkbox' name='$nameAsArray' id='$option' value='$option' $checkmark >$lineBreak";
}
echo<<<EOF
<html>
<head></head>
<body>
<form name="Update" method="GET" action="{$_SERVER['PHP_SELF']}">n
<fieldset id="tickboxes" style="width:25em;">
<legend>{$Legent}</legend>
{$boxes}
</fieldset>
<button type="submit" >Submit Form</button>
</form>
<body>
</html>
EOF
;
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.
add a comment |
Perhaps a better way is using the php function in_array() like this:
$style='V';//can be 'V'ertical or 'H'orizontal
$lineBreak=($style=='V')?'<br>':'';
$name='colors';//the name of your options
$Legent="Select your $name";//dress it up in a nice fieldset with a ledgent
$options=array('red','green','blue','orange','yellow','white','black');
$boxes='';//innitiate the list of tickboxes to be generated
if(isset($_REQUEST["$name"])){
//we shall use $_REQUEST but $_POST would be better
$Checked=$_REQUEST["$name"];
}else{
$Checked=array();
}
foreach($options as $option){
$checkmark=(in_array($option,$Checked))?'checked':'';
$nameAsArray=$name.'';//we would like the returned data to be in an array so we end with
$boxes.=($style=='V')?"<span class='label2' align='right'><b>$option : </b></span>":"<b>$option </b>";
$boxes.="<input style='width:2em;' type='checkbox' name='$nameAsArray' id='$option' value='$option' $checkmark >$lineBreak";
}
echo<<<EOF
<html>
<head></head>
<body>
<form name="Update" method="GET" action="{$_SERVER['PHP_SELF']}">n
<fieldset id="tickboxes" style="width:25em;">
<legend>{$Legent}</legend>
{$boxes}
</fieldset>
<button type="submit" >Submit Form</button>
</form>
<body>
</html>
EOF
;
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.
Perhaps a better way is using the php function in_array() like this:
$style='V';//can be 'V'ertical or 'H'orizontal
$lineBreak=($style=='V')?'<br>':'';
$name='colors';//the name of your options
$Legent="Select your $name";//dress it up in a nice fieldset with a ledgent
$options=array('red','green','blue','orange','yellow','white','black');
$boxes='';//innitiate the list of tickboxes to be generated
if(isset($_REQUEST["$name"])){
//we shall use $_REQUEST but $_POST would be better
$Checked=$_REQUEST["$name"];
}else{
$Checked=array();
}
foreach($options as $option){
$checkmark=(in_array($option,$Checked))?'checked':'';
$nameAsArray=$name.'';//we would like the returned data to be in an array so we end with
$boxes.=($style=='V')?"<span class='label2' align='right'><b>$option : </b></span>":"<b>$option </b>";
$boxes.="<input style='width:2em;' type='checkbox' name='$nameAsArray' id='$option' value='$option' $checkmark >$lineBreak";
}
echo<<<EOF
<html>
<head></head>
<body>
<form name="Update" method="GET" action="{$_SERVER['PHP_SELF']}">n
<fieldset id="tickboxes" style="width:25em;">
<legend>{$Legent}</legend>
{$boxes}
</fieldset>
<button type="submit" >Submit Form</button>
</form>
<body>
</html>
EOF
;
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.
answered May 25 '16 at 10:43
Cor CoolCor Cool
135
135
add a comment |
add a comment |
If you want to turn specific values in to new variables if they have been selected:
// Retrieve array color and set as variable
$colors = $_GET['color'];
// Use array_search to find the key for "red"
$key_red = array_search('red', $colors);
// If "red" exists, the key will be an integer (or FALSE)
if (is_int($key_red)) {
$red_color = 'Red was selected';
}
add a comment |
If you want to turn specific values in to new variables if they have been selected:
// Retrieve array color and set as variable
$colors = $_GET['color'];
// Use array_search to find the key for "red"
$key_red = array_search('red', $colors);
// If "red" exists, the key will be an integer (or FALSE)
if (is_int($key_red)) {
$red_color = 'Red was selected';
}
add a comment |
If you want to turn specific values in to new variables if they have been selected:
// Retrieve array color and set as variable
$colors = $_GET['color'];
// Use array_search to find the key for "red"
$key_red = array_search('red', $colors);
// If "red" exists, the key will be an integer (or FALSE)
if (is_int($key_red)) {
$red_color = 'Red was selected';
}
If you want to turn specific values in to new variables if they have been selected:
// Retrieve array color and set as variable
$colors = $_GET['color'];
// Use array_search to find the key for "red"
$key_red = array_search('red', $colors);
// If "red" exists, the key will be an integer (or FALSE)
if (is_int($key_red)) {
$red_color = 'Red was selected';
}
edited Nov 9 '17 at 22:26
answered Nov 5 '17 at 19:18
user8890598
add a comment |
add a comment |
foreach is the best way to get array of values.
here the example code:
html code:
<form action="send.php" method="post">
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">
<input type="submit" value="submit">
</form>
phpcode:
<?php
$name = $POST['color'];
foreach ($name as $color){
echo $color."<br />";
}
?>
cool answer !! it works
– anonymouse
Jun 19 '18 at 5:34
add a comment |
foreach is the best way to get array of values.
here the example code:
html code:
<form action="send.php" method="post">
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">
<input type="submit" value="submit">
</form>
phpcode:
<?php
$name = $POST['color'];
foreach ($name as $color){
echo $color."<br />";
}
?>
cool answer !! it works
– anonymouse
Jun 19 '18 at 5:34
add a comment |
foreach is the best way to get array of values.
here the example code:
html code:
<form action="send.php" method="post">
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">
<input type="submit" value="submit">
</form>
phpcode:
<?php
$name = $POST['color'];
foreach ($name as $color){
echo $color."<br />";
}
?>
foreach is the best way to get array of values.
here the example code:
html code:
<form action="send.php" method="post">
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">
<input type="submit" value="submit">
</form>
phpcode:
<?php
$name = $POST['color'];
foreach ($name as $color){
echo $color."<br />";
}
?>
answered May 23 '18 at 3:40
manikandanpachaiyappanmanikandanpachaiyappan
436
436
cool answer !! it works
– anonymouse
Jun 19 '18 at 5:34
add a comment |
cool answer !! it works
– anonymouse
Jun 19 '18 at 5:34
cool answer !! it works
– anonymouse
Jun 19 '18 at 5:34
cool answer !! it works
– anonymouse
Jun 19 '18 at 5:34
add a comment |
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.
<form method="post">
<input type="checkbox" name="check">This is how it works!<br>
<button type="submit" name="submit">Submit</button>
</form>
<?php
if(isset($_POST['submit'])) {
if(!isset($_POST['check'])) {
echo "Not selected!";
}else{
echo "Selected!";
}
}
?>
add a comment |
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.
<form method="post">
<input type="checkbox" name="check">This is how it works!<br>
<button type="submit" name="submit">Submit</button>
</form>
<?php
if(isset($_POST['submit'])) {
if(!isset($_POST['check'])) {
echo "Not selected!";
}else{
echo "Selected!";
}
}
?>
add a comment |
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.
<form method="post">
<input type="checkbox" name="check">This is how it works!<br>
<button type="submit" name="submit">Submit</button>
</form>
<?php
if(isset($_POST['submit'])) {
if(!isset($_POST['check'])) {
echo "Not selected!";
}else{
echo "Selected!";
}
}
?>
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.
<form method="post">
<input type="checkbox" name="check">This is how it works!<br>
<button type="submit" name="submit">Submit</button>
</form>
<?php
if(isset($_POST['submit'])) {
if(!isset($_POST['check'])) {
echo "Not selected!";
}else{
echo "Selected!";
}
}
?>
edited Oct 11 '18 at 17:29
Clyff
3,09311127
3,09311127
answered Oct 11 '18 at 16:09
ArtyArty
35
35
add a comment |
add a comment |
//retrive check box and gender value
$gender=$row['gender'];
$chkhobby=$row['chkhobby'];
<tr>
<th>GENDER</th>
<td>
Male<input type="radio" name="gender" value="1" <?php echo ($gender== '1') ? "checked" : "" ; ?>/>
Female<input type="radio" name="gender" value="0" <?php echo ($gender== '0') ? "checked" : "" ; ?>/>
</td>
</tr>
<tr>
<th>Hobbies</th>
<td>
<pre><?php //print_r($row);
$hby = @explode(",",$row['chkhobby']);
//print_r($hby);
?></pre>
read<input id="check_1" type="checkbox" name="chkhobby" value="read" <?php if(in_array("read",$hby)){?> checked="checked"<?php }?>>
write<input id="check_2" type="checkbox" name="chkhobby" value="write" <?php if(in_array("write",$hby)){?> checked="checked"<?php }?>>
play<input id="check_4" type="checkbox" name="chkhobby" value="play" <?php if(in_array("play",$hby)){?> checked="checked"<?php }?>>
</td>
</tr>
//update
$gender=$_POST['gender'];
$chkhobby = implode(',', $_POST['chkhobby']);
1
Please don't paste row code without explanation.
– Al Foиce ѫ
Aug 11 '16 at 9:11
add a comment |
//retrive check box and gender value
$gender=$row['gender'];
$chkhobby=$row['chkhobby'];
<tr>
<th>GENDER</th>
<td>
Male<input type="radio" name="gender" value="1" <?php echo ($gender== '1') ? "checked" : "" ; ?>/>
Female<input type="radio" name="gender" value="0" <?php echo ($gender== '0') ? "checked" : "" ; ?>/>
</td>
</tr>
<tr>
<th>Hobbies</th>
<td>
<pre><?php //print_r($row);
$hby = @explode(",",$row['chkhobby']);
//print_r($hby);
?></pre>
read<input id="check_1" type="checkbox" name="chkhobby" value="read" <?php if(in_array("read",$hby)){?> checked="checked"<?php }?>>
write<input id="check_2" type="checkbox" name="chkhobby" value="write" <?php if(in_array("write",$hby)){?> checked="checked"<?php }?>>
play<input id="check_4" type="checkbox" name="chkhobby" value="play" <?php if(in_array("play",$hby)){?> checked="checked"<?php }?>>
</td>
</tr>
//update
$gender=$_POST['gender'];
$chkhobby = implode(',', $_POST['chkhobby']);
1
Please don't paste row code without explanation.
– Al Foиce ѫ
Aug 11 '16 at 9:11
add a comment |
//retrive check box and gender value
$gender=$row['gender'];
$chkhobby=$row['chkhobby'];
<tr>
<th>GENDER</th>
<td>
Male<input type="radio" name="gender" value="1" <?php echo ($gender== '1') ? "checked" : "" ; ?>/>
Female<input type="radio" name="gender" value="0" <?php echo ($gender== '0') ? "checked" : "" ; ?>/>
</td>
</tr>
<tr>
<th>Hobbies</th>
<td>
<pre><?php //print_r($row);
$hby = @explode(",",$row['chkhobby']);
//print_r($hby);
?></pre>
read<input id="check_1" type="checkbox" name="chkhobby" value="read" <?php if(in_array("read",$hby)){?> checked="checked"<?php }?>>
write<input id="check_2" type="checkbox" name="chkhobby" value="write" <?php if(in_array("write",$hby)){?> checked="checked"<?php }?>>
play<input id="check_4" type="checkbox" name="chkhobby" value="play" <?php if(in_array("play",$hby)){?> checked="checked"<?php }?>>
</td>
</tr>
//update
$gender=$_POST['gender'];
$chkhobby = implode(',', $_POST['chkhobby']);
//retrive check box and gender value
$gender=$row['gender'];
$chkhobby=$row['chkhobby'];
<tr>
<th>GENDER</th>
<td>
Male<input type="radio" name="gender" value="1" <?php echo ($gender== '1') ? "checked" : "" ; ?>/>
Female<input type="radio" name="gender" value="0" <?php echo ($gender== '0') ? "checked" : "" ; ?>/>
</td>
</tr>
<tr>
<th>Hobbies</th>
<td>
<pre><?php //print_r($row);
$hby = @explode(",",$row['chkhobby']);
//print_r($hby);
?></pre>
read<input id="check_1" type="checkbox" name="chkhobby" value="read" <?php if(in_array("read",$hby)){?> checked="checked"<?php }?>>
write<input id="check_2" type="checkbox" name="chkhobby" value="write" <?php if(in_array("write",$hby)){?> checked="checked"<?php }?>>
play<input id="check_4" type="checkbox" name="chkhobby" value="play" <?php if(in_array("play",$hby)){?> checked="checked"<?php }?>>
</td>
</tr>
//update
$gender=$_POST['gender'];
$chkhobby = implode(',', $_POST['chkhobby']);
edited Aug 11 '16 at 9:44
Al Foиce ѫ
3,16462440
3,16462440
answered Aug 11 '16 at 8:51
jigarjigar
144
144
1
Please don't paste row code without explanation.
– Al Foиce ѫ
Aug 11 '16 at 9:11
add a comment |
1
Please don't paste row code without explanation.
– Al Foиce ѫ
Aug 11 '16 at 9:11
1
1
Please don't paste row code without explanation.
– Al Foиce ѫ
Aug 11 '16 at 9:11
Please don't paste row code without explanation.
– Al Foиce ѫ
Aug 11 '16 at 9:11
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f18421988%2fgetting-checkbox-values-on-submit%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
@blo i edited it.
– Nofel
Aug 24 '13 at 19:21
1
@MadaraUchiha thank you, i edited the question.
– Nofel
Aug 24 '13 at 19:22
2
Its not action="get" its method="get" !
– Rahul
Aug 24 '13 at 19:48
1
@AmmarKhan Have a look at my answer below, with a few additional options you can use.
– Funk Forty Niner
Aug 24 '13 at 23:59
1
I would suggest to use
post
method instead ofget
for form submit.– Naina
Mar 17 '14 at 8:10