简体   繁体   English

如何查看 PHP 中选中了哪些复选框?

[英]How can I see which check boxes are checked in PHP?

I'm displaying some columns from a database and at the end of each row I put a check box.我正在显示数据库中的一些列,并在每行的末尾放置一个复选框。

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>";
}

(Ignore my $checkbox variable, that's just a function parameter allowing me to toggle the check boxes on/off when I display these rows.) (忽略我的$checkbox变量,这只是一个 function 参数,允许我在显示这些行时打开/关闭复选框。)

The code above works fine, but I can't figure out how to output the value (check or unchecked) of these check boxes.上面的代码工作正常,但我不知道如何 output 这些复选框的值(选中或未选中)。 It is my understanding that all the check boxes have the same name ( selectSystem ) and that they should all be in a selectSystem array.据我了解,所有复选框都具有相同的名称( selectSystem ),并且它们都应该位于selectSystem数组中。

Maybe I'm going about this in the wrong way...I just want the user to be able to select the rows they want in order to reserve a computer system.也许我以错误的方式解决这个问题......我只是希望用户能够 select 他们想要的行以保留计算机系统。 Is there an easier way to do this or should I just continue on with my stupid check boxes that get rallied up after a submit button is clicked?有没有更简单的方法可以做到这一点,还是我应该继续使用在单击提交按钮后聚集起来的愚蠢复选框?

As Dharman suggested, use an iterator with your while loop to create your name attribute so each name is different.正如 Dharman 所建议的,在您的 while 循环中使用迭代器来创建您的 name 属性,以便每个名称都不同。 Then once your user hits the submit button, the global $_POST variable will hold the values of the checkboxes they have selected.然后,一旦您的用户点击提交按钮,全局 $_POST 变量将保存他们选择的复选框的值。

Consider the following code example..考虑以下代码示例..

// 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++;
}

OUTPUT: OUTPUT:

<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"/>

Now once you want to access these values once the user presses the submit button onthe form, the array lives in your $_POST array.现在,一旦您想在用户按下表单上的提交按钮后访问这些值,该数组就存在于您的 $_POST 数组中。 Just check a strict conditional for 'on' to access the values for the checkboxes that are checked...只需检查'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 /-->

The easiest way of handling forms is through JavaScript not directly in php because you have to use expensive work around for things like form validation and security处理 forms 的最简单方法是通过 JavaScript 而不是直接在 php 中,因为您必须使用昂贵的工作来解决表单验证和安全性等问题

If you not using some method of sanitizing the form input you open your self up to SQL injection attacks so it good to handle form values in JS and use a rest api for transferring the results to the PHP server If you not using some method of sanitizing the form input you open your self up to SQL injection attacks so it good to handle form values in JS and use a rest api for transferring the results to the PHP server

I used to do the forms like you but I realized A: its more work and B:you don't get a chance to format form responses in a way that is predictable我曾经像你一样做 forms 但我意识到 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>

Another major reason for doing things like this is that if your server is busy with a long query like handling multiple users, data base queries,or making a PDF document...这样做的另一个主要原因是,如果您的服务器正忙于处理多个用户、数据库查询或制作 PDF 文档等长查询......

you client browser will hang and wait for the server to finish.... but by using JS with xhr requests and things like await sinc the clients browser wont freeze if the server is busy with heavy computational tasks.您的客户端浏览器将挂起并等待服务器完成....但是通过使用带有 xhr 请求的 JS 和诸如 await sinc 之类的东西,如果服务器正忙于繁重的计算任务,客户端浏览器不会冻结。 Doing things this way will ensure you user interface is always active and adds to the smoothness of your program以这种方式做事将确保您的用户界面始终处于活动状态并增加程序的流畅性

I trust this help you我相信这对你有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM