【发布时间】: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