首页 > 解决方案 > 在 PHP Post 数组中插入复选框

问题描述

对不起,愚蠢的问题。我有一个带有多个动态生成的复选框的表单,我想将它们插入到 post 数组中以将它们移动到下一页然后操作它们。

表格:

<form action="campcreate.php" class="form" method="post">
<label for "campName">Campaign Name:</label>
<input type="text" id="campName" class="form-control" name="campName" placeholder="Campaign Name" required minlength="6" autofocus>
<label for "notes">Description:</label>
<textarea class="form-control" name="notes" rows="5" id="notes" placeholder="Add any useful notes or descriptions here"></textarea>
<h3>Select committee memmbers:</h3>
<p>Committee members can be changed as required,  all members selected as committee members will be able to view documents, meetings etc. connected to the campaign.</p>
<p>Assigning a committee member does <strong>not</strong> grant them any different admin privillages on the site.</p>
<?php getnames(); ?>

<div>
<button type="submit" class="btn btn-success float-right" id="create">Next</button>
<button type="button" class="btn btn-primary float-right" id="restart">Back</button>
</div> <!--Form Div--->
</form>

包含的功能:

function getnames(){
    global $conn;
$sqlga = "SELECT user_id, user_first, user_last FROM users WHERE (usr_acrive= 1) AND (perm!=0) ORDER BY user_last ASC";
$stmt = mysqli_stmt_init($conn);    
mysqli_stmt_prepare($stmt, $sqlga);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
while ($row = mysqli_fetch_assoc($result)) {
    if ($row['user_id']!=$_SESSION['user_id']){
        $checked="";
    }else{
        $checked="checked disabled";
    }
    echo '<input type="checkbox"'.$checked.' value="'. $row['user_id'] .'" name="userid"> '.$row['user_first'] . ' '. $row['user_last'] .  '<br>';
   }
}

我想将这些复选框(正确显示)转发到下一页,但我不知道如何在帖子中包含这些复选框,将自动选择一个框(创建帖子的用户)但最多可以说可以勾选其他 100 个框。

我试过回显,但我只能看到文本框。

欢迎任何指点。

标签: phphtmlmysql

解决方案


发布信息由输入标签的名称发送

您正在循环并添加具有相同名称的输入:

echo '<input type="checkbox"'.$checked.' value="'. $row['user_id'] .'" name="userid"> '.$row['user_first'] . ' '. $row['user_last'] . '<br>';

您应该在回显时尝试创建一个唯一的名称,具体取决于您需要的操作...

例如(在 foreach 循环内):

$i++;
echo '<input type="checkbox"'.$checked.' value="'. $row['user_id'] .'" name="userid"'. $i. '> '.$row['user_first'] . ' '. $row['user_last'] . '<br>'; 

推荐阅读