【问题标题】:PHP: Function() check form indexed arrayPHP:Function() 检查表单索引数组
【发布时间】:2014-11-07 13:43:58
【问题描述】:

编辑: 我解决了我的问题: 感谢大家的评论,它帮助我以不同的方式思考: 我不知道返回...;表现得这样(我相信它只是发送价值而不是结束功能),所以就像我说的,我才刚刚开始,我是一个新手...... 我用多维数组解决了我的问题,我认为它应该像我现在想要的那样工作。我在本文末尾发布了代码。

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

我正在尝试制作一个基本的检查表单功能,用于检查我未来网站上的所有表单。我花了几个小时(几天......)来完成这个简短的代码,因为我'我是 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>

我的问题是返回给输入的值总是“标题”的内容,例如如果

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

对于“标题”等于

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

再次感谢您的回答,很高兴看到即使是新手在使用此功能而不是其他功能时也不了解他们所做的事情的一半,也可以在这里获得答案。 表示赞许,满意,胜利。 问候。

【问题讨论】:

  • 这里有太多的混乱我不知道从哪里开始。 array_values 返回基于 0 的数字索引数组,因此您的 if 语句毫无意义。接下来,一个 return 语句将退出函数,所以你永远不会循环超过一次

标签: php arrays forms loops indexing


【解决方案1】:

您的代码中有很多错误:

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

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

第二:您有 2 个输入标签,名称分别为“title”和“another”,因此 $_POST 数组有 2 个索引: $_POST["title"], $_POST["another"],可以查看这个数组的内容:

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

【讨论】:

  • 首先:实际上我现在不想加载其他页面,我只想重新加载实际页面并查看是否已加载/应用正确的样式/值。第二:是的,我已经使用 var_dump 检查输入,所有输入都已正确注册。问题是,即使 $_POST = $index 中设置的值从我在 var_dump() 中看到的正确,所有输入都采用应用于 HTML 中第一个输入的样式/值。它看起来像这样:s7.postimg.org/xk02bmdjf/before.pngs7.postimg.org/gxii2jkln/after.png
猜你喜欢
  • 2012-05-27
  • 2016-11-07
  • 2021-06-18
  • 2019-05-29
  • 2010-09-20
  • 1970-01-01
  • 2021-01-19
  • 2017-03-12
  • 2018-04-17
相关资源
最近更新 更多