【问题标题】:How can I see which check boxes are checked in PHP?如何查看 PHP 中选中了哪些复选框?
【发布时间】:2020-11-14 21:03:35
【问题描述】:

我正在显示数据库中的一些列,并在每一行的末尾放置一个复选框。

while($row = $result->fetch_assoc()){
    echo "<tr><td>" . $row['Computer Name'] . '</td><td>' . $row['Computer IP'];
    echo ($checkbox)?('<td><input type="checkbox" name="selectSystem"/></td>'):'' . "</td></tr>";
}

(忽略我的$checkbox 变量,这只是一个函数参数,允许我在显示这些行时打开/关闭复选框。)

上面的代码工作正常,但我不知道如何输出这些复选框的值(选中或未选中)。据我了解,所有复选框都具有相同的名称 (selectSystem),并且它们都应位于 selectSystem 数组中。

也许我以错误的方式处理此问题...我只是希望用户能够选择他们想要的行以保留计算机系统。有没有更简单的方法来做到这一点,还是我应该继续使用在单击提交按钮后聚集起来的愚蠢复选框?

【问题讨论】:

  • 您好,$checkbox 返回是什么?它是布尔值吗?
  • 给他们唯一的名字。例如selectSystem[$i]
  • @AbedPutra,是布尔值。只是我的复选框的开/关切换

标签: php html checkbox


【解决方案1】:

正如 Dharman 所建议的,在您的 while 循环中使用迭代器来创建您的 name 属性,以便每个名称都不同。然后,一旦您的用户点击提交按钮,全局 $_POST 变量将保存他们选择的复选框的值。

考虑以下代码示例..

// define a variable to hold your output in...
$output = null;

// as I do not have your database result, I will use an example array of values
// to get a length to use in a while loop to show the iteration using a declared number
// that is iterated within your while loop until the length of the array is reached
$array = [1,2,3,4,5,6,7,8]; //--> Here we have 8 values in the array
// define a number to use as iterator or a counter
$i = 1;
// while loop iterates through array while our counter is less than the number within  our array
while($i < count($array)){
    // concatenate our output variable to include the next input 
    // Each time we iterate through to the next value in our array
    // we concatenate the next numbers iterated value by 1 to our name attribute
    // now your name attribute is uniquely named 

 
    $output .= '<input type="checkbox" name="selectSystem'.$i.'"/>';
    $i++;
}

输出:

<input type="checkbox" name="selectSystem1"/>
<input type="checkbox" name="selectSystem2"/>
<input type="checkbox" name="selectSystem3"/>
<input type="checkbox" name="selectSystem4"/>
<input type="checkbox" name="selectSystem5"/>
<input type="checkbox" name="selectSystem6"/>
<input type="checkbox" name="selectSystem7"/>
<input type="checkbox" name="selectSystem8"/>

现在,一旦您想在用户按下表单上的提交按钮后访问这些值,该数组就存在于您的 $_POST 数组中。只需检查 'on' 的严格条件即可访问已选中复选框的值...

// we define a variable to hold our results and assign a null value to it so when not set it shows nothing...
$stmt = null;
// if our $_POST isset...
if(isset($_POST)){
    // loop through results and use a strict conditional on the $value each loop through
    foreach($_POST as $key => $value){
        if($value === 'on'){
            // concatenate the variable to hold the key and value foreach result
            $stmt .= $key.' -> '.$value.'<br>';
        }
    };
}

<form method="post">
    <?=$output?><!--/ Echo your $output which holds the inputs/-->
    <input type="submit" value="submit" name="submit">
</form>

<?=$stmt?><!--/ Echo your $stmt which holds the $_POST array values of your checkboxes that are 'on', if this variable is null it shows nothing in your html /-->

【讨论】:

  • 感谢您抽出宝贵时间将其输入 Dale,我现在可以看得更清楚了。我没有将 $i 附加到复选框名称,而是附加了主键!可能不是最佳实践,但稍后会少一个映射步骤
  • 我仍然无法访问这些值。这是我的更新时间:echo "&lt;td&gt;&lt;input type='checkbox' name='selectSystem".$row['Computer Name']."'/&gt;&lt;/td&gt;"; $checkboxArray .= "&lt;input type='checkbox' name='selectSystem".$row['Computer Name']."'/&gt;"; 在我回显复选框后,我将复选框(现在具有唯一名称)保存到 checkboxArray。所以现在我有一系列复选框,但我似乎仍然无法访问它们的值
  • 这是我的 if isset 块:$stmt = null; if(isset($_POST['reserve'])){ foreach($_POST as $key =&gt; $value){ if($value ==='on') $stmt .= $key ."-&gt;".$value."&lt;br&gt;"; } echo $checkboxArray; }
  • 提交表单后,您的 $_POST 数组会得到什么? var_dump($_POST)?
  • echo var_dump($_post) 得到我:array(1) { ["reserve"]=> string(7) "RESERVE" } ...我在某个函数中打印所有复选框。它们都具有基于行的确定值....稍后在代码中我打印出带有表单标签的提交按钮。我打印的复选框也需要表单标签吗?
【解决方案2】:

处理表单最简单的方法是通过 JavaScript 而不是直接在 php 中,因为您必须使用昂贵的工作来解决表单验证和安全性等问题

如果你不使用某种方法来清理表单输入,你就会对 SQL 注入攻击敞开心扉,所以最好在 JS 中处理表单值并使用 rest api 将结果传输到 PHP 服务器

我曾经像你一样做表单,但我意识到 A:它需要更多的工作和 B:你没有机会以可预测的方式格式化表单响应

//get the button event 
const btn = document.getElementById('btn');
//event
        btn.onclick = () => {
           //  get the element and see if it is checked   
          let cb = document.getElementById('accept');
          console.log(cb.checked);
  /// once all your form data is prepped send to the server through an XHR request 
//PHP rest API should handle the receiving side

   
          
        };
<form>
        <input type="checkbox" id="accept" checked> Accept
        <input type="button" id="btn" value="Submit">
    </form>

这样做的另一个主要原因是,如果您的服务器忙于处理多个用户、数据库查询或制作 PDF 文档等长查询......

您的客户端浏览器将挂起并等待服务器完成....但是通过使用带有 xhr 请求的 JS 和诸如 await sinc 之类的东西,如果服务器正忙于繁重的计算任务,客户端浏览器不会冻结。以这种方式做事将确保您的用户界面始终处于活动状态并增加程序的流畅度

我相信这对你有帮助

【讨论】:

  • 前端验证很容易绕过。是的,它对用户更友好,但后端验证对安全性更重要
  • 我同意后端 php 是安全性最重要的地方......就应用程序的流畅性而言,最好使用 JS,特别是如果 PHP 服务器有大量流量并忙于处理紧张的 CPU 工作。在这种情况下,将接口 cpu 工作传递给客户端并让服务器 cpu 来执行数据库事务是有益的
  • @dale 兰德里在下面指出的是一种仅使用 php 执行此操作的方法,并且在为每一行添加唯一键以从该特定行中提取数据方面是正确的,所以我怀疑这就是提出问题的人正在寻找的东西
猜你喜欢
  • 2011-01-17
  • 2020-09-18
  • 1970-01-01
  • 1970-01-01
  • 2016-07-28
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
相关资源
最近更新 更多