php 在提交时获取复选框值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18421988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting checkbox values on submit
提问by Nofel
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?
我有 6 个选项,我想获取选中的值以将它们存储在第二页的变量中。我如何继续这样做?
<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.phppage :
和third.php页面:
$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 :
如果我删除[],我会看到颜色,当我这样做时,color[]我会收到一条通知说:
Array to string conversion
数组到字符串的转换
What I want is the value of checked, checkboxes so I can store it in variable.
我想要的是选中的复选框的值,以便我可以将其存储在变量中。
回答by Funk Forty Niner
A good method which is a favorite of mine and for many I'm sure, is to make use of foreachwhich will output each color you chose, and appear on screen one underneath each other.
一个很好的方法是我最喜欢的,我敢肯定,对许多人来说,使用foreach它会输出您选择的每种颜色,并在屏幕上显示一个彼此下方。
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.
当谈到使用复选框时,您别无选择,只能使用foreach,这就是为什么您只能从数组中返回一个值。
Here is an example using $_GET. You can however use $_POSTand would need to make both directives match in both files in order to work properly.
这是一个使用$_GET. 但是$_POST,您可以使用并且需要使两个文件中的两个指令都匹配才能正常工作。
HTML FORM
HTML 表单
<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.phpas your handler
PHP(使用 $_GET)third.php用作您的处理程序
<?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
选项#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>)
显示为列表:(<ul></ul>可以替换为<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.";
}
?>
回答by Rahul
(It's not action="get"or action="post"it's method="get"or method="post"
(它不是action="get"或action="post"它是method="get"或method="post"
Try to do it using post method:
尝试使用 post 方法来做到这一点:
<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
并在第三个.php
or for a pericular field you colud get value in:
或者对于特定领域,您可以在以下方面获得价值:
$_POST['color'][0] //for RED
$_POST['color'][1] // for GREEN
回答by Sathya Baman
What i suggest is , its better to use post than get. here are some difference between post VS get
我的建议是,使用 post 比 get 更好。这是 post VS get 之间的一些区别
Some notes on GET requests:
关于 GET 请求的一些注意事项:
- 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
- GET 请求可以被缓存
- GET 请求保留在浏览器历史记录中
- 可以为 GET 请求添加书签
- 处理敏感数据时不应使用 GET 请求
- GET 请求有长度限制
- GET 请求应仅用于检索数据
Some notes on POST requests:
关于 POST 请求的一些注意事项:
- 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
- POST 请求永远不会被缓存
- POST 请求不会保留在浏览器历史记录中
- 无法为 POST 请求添加书签
- POST 请求对数据长度没有限制
HTML Code
HTML代码
<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代码
<?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.";
}
?>
回答by manikandanpachaiyappan
foreach is the best way to get array of values.
foreach 是获取值数组的最佳方式。
here the example code: html code:
这里的示例代码: html 代码:
<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 />";
}
?>
回答by manikandanpachaiyappan
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!";
}
}
?>
回答by Vaibhavraj Roham
Just for printing you can use as below :
仅用于打印,您可以使用如下:
print_r($_GET['color']);
or
或者
var_dump($_GET['color']);
回答by Blacboy
I think the value for the $_POST['color']should be read only after checking if its set.
我认为$_POST['color']只有在检查它是否设置后才应该读取它的值。
<?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.";
}
?>
回答by Cor Cool
Perhaps a better way is using the php function in_array()like this:
也许更好的方法是使用 php 函数in_array()像这样:
$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 $styleto set if we want the options in a horizontal or vertical way. This will infrequence how we display our checkboxes. Next we set the $namefor 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.
首先,我们创建了一个变量$style来设置我们是否想要水平或垂直方式的选项。这将不常显示我们的复选框。接下来我们$name为我们的选项设置,这需要作为我们想要保留我们的选项的数组的名称。我在这里创建了一个循环来构造数组中给定的每个选项$options,然后我们检查每个项目是否应该在返回的表单中进行检查。我相信这应该可以简化我们使用复选框重现表单的方式。
回答by Cor Cool
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';
}
回答by jigar
//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']);

