dongye9991 2015-07-10 15:53
浏览 15
已采纳

我怎么能用php发布复选框?

This is my form and categories comes from the database with while loop. I want to insert the checked inputs only in to database.

How can i detect which checkbox is selected ?

<form action="account.php" method="POST">
    <ul class="account-info">
        <li>Category1 : <input type="checkbox" value="val1" name="cat1"></li>
        <li>Category2 : <input type="checkbox" value="val2" name="cat1"></li>
        <li>Category3 : <input type="checkbox" value="val3" name="cat1"></li>
        <!-- while continues -->
        <li>Category100 : <input type="checkbox" value="val100" name="cat1"></li>
    </ul>
    <input type="submit" value="submit" />
</form>
  • 写回答

4条回答 默认 最新

  • dongwei3712 2015-07-10 15:57
    关注

    In account.php only the check boxes will be posted. As you've named them all the same though, only 1 will be posted, the last checkbox. If you want them to have the same name and come through as an array you need to add [] after the name, like this:

    <input type="checkbox" value="val100" name="cat1[]">
    

    Then in your account.php where they are submitted you can do this:

    foreach($_POST['cat1'] as $val)
    {
        echo "$val<br>";
    }
    

    That will echo out the values of all the checked boxes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

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