提交时获取复选框值 [英] Getting checkbox values on submit

查看:107
本文介绍了提交时获取复选框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有6个选项,我想获取选中的值,将它们存储在第二页的变量中。

 < form action =third.phpmethod =get > 
<! - - 选择 - >
Red< input type =checkboxname =color []id =colorvalue =Red>
Green< input type =checkboxname =color []id =colorvalue =Green>
Blue< input type =checkboxname =color []id =colorvalue =Blue>
Cyan< input type =checkboxname =color []id =colorvalue =Cyan>
Magenta< input type =checkboxname =color []id =colorvalue =Magenta>
黄色 Black< input type =checkboxname =color []id =colorvalue =Black>
<! - 提交 - >
< input type =submitvalue =submit>
< / form>

third.php 页面:

  $ color = $ _GET ['color']; 

echo'颜色是'。$ color;

如果我删除 [] ,我会得到当我这样做的时候颜色是开的,比如 color [] 我得到一个通知:


数组到字符串转换

我想要的是选中的复选框的值,以便我可以将它存储在变量中。 / p>

解决方案

一个很好的方法是我最喜欢的方法,对于很多我相信的方法是利用 foreach ,它将输出你选择的每种颜色,并在屏幕上出现在彼此之下。

在使用复选框时,您没有选择,只能使用 foreach ,这就是为什么你只能从你的数组中返回一个值。



这是一个使用 $ _ GET 的例子。但是,您可以使用 $ _ POST ,并且需要使两个文件中的两个指令匹配才能正常工作。



HTML FORM



 < form action =third.phpmethod =get> 
红色< input type =checkboxname =color []id =colorvalue =red>
Green< input type =checkboxname =color []id =colorvalue =green>
Blue< input type =checkboxname =color []id =colorvalue =blue>
Cyan< input type =checkboxname =color []id =colorvalue =cyan>
Magenta< input type =checkboxname =color []id =colorvalue =Magenta>
黄色< input type =复选框name =color []id =colorvalue =yellow>
Black< input type =checkboxname =color []id =colorvalue =black>
< input type =submitvalue =submit>
< / form>



PHP(使用$ _GET)使用 third.php 作为您的处理程序



 <?php 

$ name = $ _GET ['color' ]。

//可选
//回显您选择了以下颜色:< br>;

foreach($ name as $ color){
echo $ color。< br />;
}

?>

假设已选择红色,绿色,蓝色和青色作为颜色,将如下所示:



红色

绿色

蓝色

青色



< hr>

选项#2



您也可以检查是否选择了颜色。

 <?php 

$名称= $ _GET ['color'];

if(isset($ _ GET ['color'])){
echo您选择了以下颜色:< br>;

foreach($ name as $ color){
echo $ color。< br />;
}
} else {
echo你没有选择颜色。;
}

?>






附加选项:



显示为列表:(< ul>< / ul> 可以替换为< ol> ;< / ol>

 <?php 

$ name = $ _GET ['color'];

if(isset($ _ GET ['color'])){
echo您选择了以下颜色:< br>;
回显< ul>;
foreach($ name为$ color){
echo< li> 。$颜色 < /锂> 中。
}
echo< / ul>;
} else {
echo你没有选择颜色。;
}

?>






标志:( 2016年8月5日)



今天早上我收到了2笔积分,我的评论被删除了;为什么?无论谁这样做,都明显地以恶意行事。答案没有收到任何东西,只是upvotes和现在这个。我的意思是认真的跆拳道? - Fred -ii- 1小时前拒绝 - 人们可以自由投票,只要你没有被定位。我没有看到这方面的证据。您对投票的评论是噪音,标记为这样,并被删除。



回答这个问题: 我是。

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.

解决方案

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.

这篇关于提交时获取复选框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆