在 PHP 中检查一个框是否被选中的函数

我试图掌握如何正确理解 POST 的工作原理,并决定制作一个生成 100 个复选框的小 PHP 程序,然后单击文档顶部的按钮,调用一个函数来循环所有复选框并说明它们是否被选中。但是,我实施它的方式似乎不起作用。

这是我在编写此脚本时所想的更循序渐进的过程,如果有人能告诉我我的想法中的缺陷,我将不胜感激。

  1. 在带有 的表单中创建一个按钮method=post

  2. 在 PHP 中创建一个数组来存储所有复选框名称。

  3. 创建一个 for 循环。在循环的每次迭代中,都会创建一个格式为 的字符串,#checkbox因此每个复选框都有自己的唯一标识符。这些字符串中的每一个都存储在一个数组中。PHP 然后回显一个表单,method=post每个表单内都有一个复选框。每个复选框都有其唯一的名称。

  4. checkFunction()创建每个复选框后,您可以通过单击调用该函数的按钮来查看每个复选框是否被选中。

  5. 在函数中,有一个foreach循环。此循环遍历数组中的每个元素以获取复选框名称,用于isset查看复选框是否已选中,如果选中则回显。

这对我来说似乎很简单,但无论出于何种原因,它似乎都不起作用。谁能解释为什么不呢?

<!DOCTYPE html>

<html>

    <head>

        <title>Checked Boxes</title>

    </head>

    <body>

        <form method="post"> 

            <input type="submit" name="submit_checked" class="button" value="Check if checkbox is checked"/> 

        </form> 


        <?php

            $checkboxNames = array();

            for ($x = 1; $x <= 100; $x++) {

                $checkboxStr=$x."checkbox";

                $checkboxNames[] = $checkboxStr;

                echo "<form method=\"post\"><input type=\"checkbox\" name=\"$checkboxStr\" value=\"$checkboxStr\">$checkboxStr</form>";

            }


            echo("<hr>");

            echo("<h2>Status of Checkboxes:</h2>");


            function checkFunction(){

                foreach ($checkboxNames as $currentName){

                    if (isset($_POST["$currentName"]))

                        echo("<p>$currentName is checked!</p>");

                    else

                        echo("<p>$currentName is not checked!</p>");

                }

            }


            if(isset($_POST['submit_checked']))

                checkFunction();

        ?>

    </body>

</html>


杨魅力
浏览 179回答 2
2回答

RISEBY

您需要删除表单中的标签foreach,您应该只<form method="post">&nbsp;在表单中使用 1。并将复选框输入放在主表单中;PHP 函数无法访问该函数之外的变量,因此您应该将该变量放在参数中。下面是工作代码:<!DOCTYPE html><html><head>&nbsp; &nbsp; <title>Checked Boxes</title></head><body><form method="post">&nbsp; &nbsp; <input type="submit" name="submit_checked" class="button" value="Check if checkbox is&nbsp; &nbsp; checked"/><br>&nbsp; &nbsp; <?php&nbsp; &nbsp; $checkboxNames = array();&nbsp; &nbsp; for ($x = 1; $x <= 100; $x++) {&nbsp; &nbsp; &nbsp; &nbsp; $checkboxStr&nbsp; &nbsp; &nbsp;= $x."checkbox";&nbsp; &nbsp; &nbsp; &nbsp; $checkboxNames[] = $checkboxStr;&nbsp; &nbsp; &nbsp; &nbsp; echo "<input type=\"checkbox\" name=\"$checkboxStr\" value=\"$checkboxStr\">$checkboxStr <br>";&nbsp; &nbsp; }&nbsp; &nbsp; echo("<hr>");&nbsp; &nbsp; echo("<h2>Status of Checkboxes:</h2>");&nbsp; &nbsp; function checkFunction($checkboxNames)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; foreach ($checkboxNames as $currentName) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (isset($_POST[$currentName])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo("<p>$currentName is checked!</p>");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo("<p>$currentName is not checked!</p>");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if (isset($_POST['submit_checked'])) {&nbsp; &nbsp; &nbsp; &nbsp; checkFunction($checkboxNames);&nbsp; &nbsp; }&nbsp; &nbsp; ?></form></body></html>

繁花如伊

您仅在所有复选框都在未提交的单独表单中时才提交第一个表单。尝试重写循环以在主表单中包含复选框输入。
打开App,查看更多内容
随时随地看视频慕课网APP