PHP 获取多个选中复选框的标签文本


php get label text of multiple checked checkboxes

这是一个扩展问题:从多个复选框中获取 _POST 美元

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

"销售 |工程"(不带引号)

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

"销售"

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

你应该使用数组类型命名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"]);
 }
?>

缺少值...使用以下

<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/>
首先将这些

标签指定为复选框的值,其次将复选框命名为数组。如果不像这样命名所有复选框,您将不会像在循环中期望的那样获得多个值。

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

没关系 - 我作为dumb@$$。我没有关闭表单中的"name"属性。一切正常 - 谢谢大家的投入。George PHP有一个稍微好一点的解决方案。