Php 在提交时获取复选框值
我有6个选项,我想得到选中的值,将它们存储在第二页的变量中。我如何继续这样做?Php 在提交时获取复选框值,php,forms,Php,Forms,我有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="c
<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>
如果我删除[]
,我得到的颜色是开的,当我像color[]
那样做时,我会得到一个通知,上面说:
数组到字符串的转换
我想要的是选中复选框的值,这样我就可以将其存储在变量中。(它不是action=“get”
或action=“post”
而是method=“get”
或method=“post”
尝试使用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>
一个很好的方法是使用
foreach
,它将输出您选择的每种颜色,并在屏幕上一个接一个地显示出来,这是我的最爱,我相信对许多人来说也是如此
在使用复选框时,除了使用foreach
,您别无选择,这就是为什么您只能从数组返回一个值
下面是一个使用$\u GET
的示例。但是,您可以使用$\u POST
,并且需要使两个文件中的两个指令都匹配才能正常工作
###HTML表单
假设选择了红色、绿色、蓝色和青色作为颜色,将显示如下:
$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
;
红色绿色
蓝色
青色
##选项2 您还可以检查是否选择了颜色。如果未选择任何颜色,则会显示单独的消息
<?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.";
}
?>
##其他选择: 显示为列表:(
可替换为
)
我的建议是,使用post比get更好。这里是post和get之间的一些区别 关于GET请求的一些注意事项:
<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代码
<?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.";
}
?>
仅用于打印,您可以按如下方式使用:
print_r($_GET['color']);
或
我认为$\u 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.";
}
?>
也许更好的方法是像这样使用php函数:
$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
;
$style='V';//可以是垂直的或水平的
$lineBreak=($style='V')?“
”:”;
$name='colors';//选项的名称
$Legent=“选择您的$name”;//将其装饰在一个漂亮的字段集中,并带有一个台帐
$options=array('red','green','blue','orange','yellow','white','black');
$box='';//初始化要生成的勾选框列表
如果(isset($_请求[“$name”]){
//我们将使用$u请求,但$u POST会更好
$Checked=$_请求[“$name”];
}否则{
$Checked=array();
}
foreach($options作为$option){
$checkmark=(在数组中($option,$Checked))?“Checked”:“;
$nameAsArray=$name.'[];//我们希望返回的数据位于数组中,因此以[]结尾
$box.=($style=='V')?“$option::“$option”;
$box.=“$lineBreak”;
}
echo//检索复选框和性别值
$SEXT=$row['SEXT'];
$chkhobby=$row['chkhobby'];
性别
男性/>
业余爱好
阅读>
写>
播放>
//更新
$gender=$_POST['gender'];
$chkhobby=内爆(',',$_POST['chkhobby']);
如果要将特定值转换为新变量(如果已选择),请执行以下操作:
<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>
foreach是获取值数组的最佳方法
下面是示例代码:
html代码:
<?php
$name = $POST['color'];
foreach ($name as $color){
echo $color."<br />";
}
?>
红色
绿色
蓝色
青色
洋红
黄色的
黑色
phpcode:
<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!";
}
}
?>
这很简单
复选框字段类似于输入文本。如果您不在该字段中写入任何内容,它会说该字段不存在
这就是它的工作原理!
提交
@MadaraUchiha谢谢,我编辑了这个问题。它不是action=“get”它的method=“get”!@AmmarKhan看看我下面的答案,还有一些额外的选项可以使用。我建议使用post
方法而不是get
进行表单提交。为什么在表单中使用多个id
呢?二维数组有什么特点?get是否支持类似这样的东西:
可以与method一起使用=Postah,所以这是PHP开始出现问题的时候了。$\u POST是一个数组,当查看解决方案时,我想,“$\u POST['color']也是它自己的数组,不是吗?”我知道这是一个古老的作品,但我对所有的id
s都是彩色的感到有点局促不安。我一直认为这是一个很大的禁忌-或者对于一个收音机或复选框组来说,这是一个好办法吗?name
s都是相同的是最简单的解决方案,但是id
s是操纵DOM来预设表单的最安全的方法tc.当我看到相同的id时,我只是听到人们在坟墓里翻滚!@BeNice我明白你的意思,但是在PHP中,如果需要名称属性,就不能单独传递“id”。我倾向于认为需要JS。哦,我漏掉了重要的词-jQuery/JavaScript。我通常会做一点jQuery,然后在相关框中打勾。ID很重要,因为在任何选择器中,它们的“权重”最强的是JavaScript。无论如何,这个答案已经存在了6年,没有人抱怨,所以我会闭嘴。回答很好,很简洁。@BeNice我同意。你的评论我们有充分的理由,谢谢。
<?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.";
}
?>
$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
;
//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']);
// 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';
}
<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>
<?php
$name = $POST['color'];
foreach ($name as $color){
echo $color."<br />";
}
?>
<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!";
}
}
?>