dongyuedaochen8415 2013-10-04 13:49
浏览 108
已采纳

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

This is an extended question from: Get $_POST from multiple checkboxes

I am trying to get the labels of each checked checkbox, then display it as such:

"Sales | Engineering" (without the quotes)

Or if only one checkbox was selected:

"Sales"

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 (result.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>

*Edited - the "if" statement in the php file always returns false even if multiple checkboxes are checked

  • 写回答

4条回答 默认 最新

  • doufang7385 2013-10-04 13:56
    关注

    You should use array type naming loc_chks[], this also lets you use specific keys for each item, for example: loc_chks[sales] and with PHP you have it in $_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/>
    

    In PHP you can use implode funciton, which lets you concatenate array items with the glue string (as first parameter)
    PHP (result.php):

    <?php
    
    session_start();
    
     if(!empty($_POST["loc_chks"]) and is_array($_POST["loc_chks"])) {
         $loc = implode(' | ',$_POST["loc_chks"]);
     }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题