PHP:Function()检查表单索引数组


PHP: Function() check form indexed array

编辑:我解决了我的问题:感谢大家的评论,它帮助我以不同的方式思考:我不知道那个回报。。。;行为是这样的(我认为这只是发送值,而不是结束函数),所以就像我说的,我只是一个新手。。。我用多维数组解决了我的问题,我认为它应该像我现在想要的那样工作。我把代码贴在了这篇文章的末尾。

-----------------------------------------------------------

我正在尝试制作一个基本的检查表单功能,用于检查我未来网站上的所有表单。我花了几个小时(几天…)才完成这段短代码,因为我是PHP新手,几天前才开始。这是想法(注意表单要大得多,我只是创建了一个较小的版本用于调试)。HTML页面中require()附带的PHP库:

function check_form() {
    $n = 0;
    $indexed = array_values($_POST);
    // How should I do to make this loop,
    // to edit the HTML of each input one by one,
    // without modifying other input that the one corresponding
    // to the $n position in the table ?
    if (isset($indexed[$n])) {
        if (!empty($indexed[$n])) {
            $value = $indexed[$n];
            $icon = define_article_style()[0];
            $color = define_article_style()[1];
        } else {
            $value = define_article_style()[4];
            $icon = define_article_style()[2];
            $color = define_article_style()[3];
        }
        $form_data_input = array($value, $icon, $color);
        return $form_data_input;
    }
}

HTML表单:

        <form role="form" method="post" action="#checked">
            <div class="form-group <?php echo check_form()[1]; ?>">
                <label class="sr-only" for="title">Title</label>
                <input type="text" name="title" class="form-control input-lg" id="title"  placeholder="Enter title * (min: 5 characters, max: 30 characters)" value="<?php echo check_form()[0]; ?>">
                <?php echo check_form()[2]; ?>
            </div>
            <div class="form-group <?php echo check_form()[1]; ?>">
                <label class="sr-only" for="another">Title</label>
                <input type="text" name="another" class="form-control input-lg" id="another"  placeholder="Enter title * (min: 5 characters, max: 30 characters)" value="<?php echo check_form()[0]; ?>">
                <?php echo check_form()[2]; ?>
            </div>
            <button type="submit" class="btn btn-default" name="check_article" value="#checked">Check</button>
        </form>

我的问题是,返回到输入的值总是"title"的内容,因此例如,如果

    $array[0] = $indexed[$index];
    $array[1] = define_article_style()[0];
    $array[2] = define_article_style()[1];

"title"的等于

    $array[0] = "blabla";
    $array[1] = "myclass1";
    $array[2] = "myclass2";

表单的每个其他输入(在这种情况下,它只是"另一个")都具有相同的值。我有点困惑,我不太清楚一切是如何工作的,但我认为这与输入共享$array[n]以获得其值/风格的事实有关,但我真的不明白如何保持这个概念并使其工作。所以,如果你理解这里不起作用的地方,我会对解释感兴趣(请记住,我是代码和PHP的新手。)非常感谢。问候

-----------------------------------------------------------

这是我的工作代码(我必须验证,但我认为它工作正常)。

function check_form() {
    $n = 0;
    $index_post = array_values($_POST);
    while ($n < count($index_post)) {
        if (isset($index_post[$n])) {
            if (!empty($index_post[$n])) {
                $value[$n] = $index_post[$n];
                $color[$n] = define_article_style()[0];
                $icon[$n] = define_article_style()[1];
            } else {
                $value[$n] = define_article_style()[4];
                $color[$n] = define_article_style()[2];
                $icon[$n] = define_article_style()[3];
            }
        }
        $array_all = [$value, $color, $icon];
        $n = $n + 1;
    }
    return $array_all;
}

再次感谢你的回答,很高兴看到即使是新手在使用这个函数而不是其他函数时,也会在这里得到答案。砰的一声。问候。

您的代码中有很多错误:

首先:我假设您有一个视图文件"form.php"answers"submit_form.php"中提交表单的过程,所以在您的html代码中,您需要修复此(操作属性):

 <form role="form" method="post" action="submit_form.php">

第二个:您有两个名称分别为"title"answers"other"的输入标记,因此$_POST数组有两个索引:$_POST["title"],$_POSD["other"],您可以检查此数组的内容:

var_dump($_POST); // this function print all the data of this array, check it.