【问题标题】:php get label text of multiple checked checkboxesphp获取多个选中复选框的标签文本
【发布时间】:2013-10-04 22:18:42
【问题描述】:

这是来自Get $_POST from multiple checkboxes的扩展问题

我正在尝试获取每个选中复选框的 标签,然后将其显示为:

“销售 | 工程”(不带引号)

或者如果只选择了一个复选框:

“销售”

HTML (myform.html):

<!DOCTYPE html>
<html>
<head></head>
<body>
<form name='myform' method='post' action='result.php'>
<label for='sales'>Sales</label>
<input type='checkbox' name='loc_chks[]' id='sales' value='Sales' /><br/>

<label for='insideSales'>Inside Sales</label>
<input type='checkbox' name='loc_chks[]' id='insideSales' value='Inside Sales' /><br/>

<label for='engineering'>Engineering</label>
<input type='checkbox' name='loc_chks[]' id='engineering' value='Engineering' /><br/>

<label for='fieldService'>Field Service</label>
<input type='checkbox' name='loc_chks[]' id='fieldService' value='Field Service' /><br/>

<label for='production'>Production</label>
<input type='checkbox' name='loc_chks[]' id='production' value='Production' /><br/>

<label for='warehouse'>Warehouse</label>
<input type='checkbox' name='loc_chks[]' id='warehouse' value='Warehouse'/><br/>
<br/>
<input type='submit' id='subBtn' value='Submit'/>
</form>

</body>
</html>

PHP(结果.php):

 <!DOCTYPE html>
 <html>
 <head></head>
 <body>
    <p><?php 
          if(!empty($_POST["loc_chks"]) and is_array($_POST["loc_chks"])) {
            echo implode(' | ',$_POST["loc_chks"]);
          } else { echo "we have a loser";}

        ?>
    </p>
 </body>
 </html>

*已编辑 - php 文件中的“if”语句总是返回 false,即使选中了多个复选框

【问题讨论】:

    标签: php checkbox foreach labels


    【解决方案1】:

    您应该使用数组类型命名 loc_chks[],这也可以让您为每个项目使用特定的键,例如:loc_chks[sales] 并且使用 PHP 您可以在 $_POST["loc_chks"]["sales"] 中使用它
    HTML (myform.html):

    <label for='sales'>Sales</label>
    <input type='checkbox' name='loc_chks[]' id='sales' value='Sales' /><br/>
    
    <label for='insideSales'>Inside Sales</label>
    <input type='checkbox' name='loc_chks[]' id='insideSales' value='Inside Sales' /><br/>
    
    <label for='engineering'>Engineering</label>
    <input type='checkbox' name='loc_chks[]' id='engineering' value='Engineering' /><br/>
    
    <label for='fieldService'>Field Service</label>
    <input type='checkbox' name='loc_chks[]' id='fieldService' value='Field Service' /><br/>
    
    <label for='production'>Production</label>
    <input type='checkbox' name='loc_chks[]' id='production' value='Production' /><br/>
    
    <label for='warehouse'>Warehouse</label>
    <input type='checkbox' name='loc_chks[]' id='warehouse' value='Warehouse' /><br/>
    

    在 PHP 中,您可以使用 implode 函数,它可以让您将数组项与粘合字符串(作为第一个参数)连接起来
    PHP(结果.php):

    <?php
    
    session_start();
    
     if(!empty($_POST["loc_chks"]) and is_array($_POST["loc_chks"])) {
         $loc = implode(' | ',$_POST["loc_chks"]);
     }
    
    ?>
    

    【讨论】:

    • 由于某种原因,此 if 语句返回 FALSE ..我认为这与我的设置有关。我将编辑我的原始帖子以显示完整文件。
    【解决方案2】:

    缺少值...使用以下

    <label for='sales'>Sales</label>
     <input type='checkbox' name='loc_chks[]' id='sales' value='Sales' /><br/>
    
     <label for='insideSales'>Inside Sales</label>
    <input type='checkbox' name='loc_chks[]' id='insideSales' value='Inside Sales' /><br/>
    
    <label for='engineering'>Engineering</label>
    <input type='checkbox' name='loc_chks[]' id='engineering' value='Engineering' /><br/>
    
    <label for='fieldService'>Field Service</label>
    <input type='checkbox' name='loc_chks[]' id='fieldService' value='Field Service' /><br/>
    
    <label for='production'>Production</label>
    <input type='checkbox' name='loc_chks[]' id='production' value='Production' /><br/>
    
    <label for='warehouse'>Warehouse</label>
    <input type='checkbox' name='loc_chks[]' id='warehouse' value='Warehouse'/><br/>
    

    【讨论】:

    • 这看起来很有希望。让我试试看。
    • 如果用户只选择了一个怎么办?或者如果用户选择了三个呢?
    【解决方案3】:

    首先将这些标签指定为复选框的值,然后将复选框命名为数组。如果不这样命名所有复选框,您将不会在循环中获得预期的多个值。

    <label for='sales'>Sales</label>
    <input type='checkbox' name='loc_chks[]' id='sales' value="Sales"/><br/>
                                         ^^             ^       
    

    【讨论】:

      【解决方案4】:

      没关系 - 我很笨@$$。我没有关闭表单中的“名称”属性。一切正常 - 谢谢大家的意见。 George PHP 有一个稍微好一点的解决方案。

      【讨论】:

        猜你喜欢
        • 2019-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-24
        • 1970-01-01
        • 2020-07-15
        相关资源
        最近更新 更多