doudi2431 2018-07-25 08:27
浏览 43
已采纳

在另一个循环中保存一次复选框值

I have this code:

<?php
if(isset($_POST['submit'])) {
 if(isset($_POST['sharks'])) {
      $_SESSION['value'] = $_POST['sharks'];
  } else {
      $_SESSION['value'] = '';
  }
}
?>
<form action="" method="POST">
  <?php
  echo '<input name="sharks" type="checkbox" value="1" id="sharks" ';
    if ($_SESSION['value'] == 1) {
      echo ' checked="checked"';
    }
  echo ">";
  ?>
  <br>
  <button type="submit" name="submit" value="Save">Salva</button>
</form>

I'm already inside a loop of users, I just want to add a checkbox next to them and save the value of that checkbox connected to that user.
enter image description here

When I try to save the checkbox this is what happen:
enter image description here

Thank you all

UPDATE: This image can help a lot: https://i.imgur.com/vXjSqcF.png
This is the table I have, I just want to add a checkbox next to every user and save the status of them.

  • 写回答

3条回答 默认 最新

  • douju2014 2018-07-25 10:25
    关注

    It's hard to make an answer based on assumption. I assume that your field names are identical. In general, in case of identical input names either all values shall be checked at once when you select one or only one value shall be checked based on conditional submit. This is usual as you are not defining unique input names for the users. However, In order to select as much users as you need follow the code structure below:

    <?php
    if(isset($_POST['submit'])){ $_SESSION['mysession'] = $_POST; }else{ $_SESSION['mysession'] = array('user1'=>'', 'user2'=>'', 'user2'=>'', 'user3'=>'');}
    ?>
    
    <form action="<?php echo $_SERVER["PHP_SELF"];?>" method="POST"> 
    <input type="checkbox" name="user1" value="1" <?php if (isset($_POST['user1'])){ echo "checked";}?> > 
    <input type="checkbox" name="user2" value="2" <?php if (isset($_POST['user2'])){ echo "checked";}?> > 
    <input type="checkbox" name="user3" value="3" <?php if (isset($_POST['user3'])){ echo "checked";}?> > 
    <button type="submit" name="submit" value="Save">Try</button>
    </form>
     
    <?php var_dump($_SESSION['mysession']); ?>

    If you need to insert the value 1 into database for all users just change the value to value="1" for every check box input.

    So you don't not need separate save button for every check box. I hope that you shall definitely get the desired output.

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

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突