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条)

报告相同问题?

悬赏问题

  • ¥30 求给定范围的全体素数p的(p-2)的连乘积
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页