php echo隐藏表单字段

时间:2013-08-02 09:48:16

标签: php loops foreach hidden-field

我试图从一些隐藏的文本字段中提取值,但不太确定如何操作。

字段存储在一个数组中,然后循环输出。

我有几个这样的字段:

<input type="hidden" name="variable_post_id[0]" value="1336"/>
<input type="hidden" name="variable_post_id[1]" value="1337"/>
<input type="hidden" name="variable_post_id[2]" value="1338"/>

我如何从上面提取值?我尝试过以下但没有快乐:

$posts =  $_REQUEST['variable_post_id'];
foreach ($posts as $post) {
   echo $post;

}

5 个答案:

答案 0 :(得分:0)

尝试

$posts =  $_POST['variable_post_id'];

并确保您将这些隐藏字段保留在表单中,并且您的html就像

<input type="hidden" name="variable_post_id[]" value="1336"/>

答案 1 :(得分:0)

你的代码看起来很好..但你不必在输入html中添加索引,确保这些字段在<form>标记内,你可以使用$_POST$_GET而不是根据你的表格方法要求

试试这个。

<input type="hidden" name="variable_post_id[]" value="1336"/>
<input type="hidden" name="variable_post_id[]" value="1337"/>
<input type="hidden" name="variable_post_id[]" value="1338"/>


$posts =  $_REQUEST['variable_post_id'];
foreach ($posts as $post) {
 echo $post;

}

答案 2 :(得分:0)

尝试不带数字

<input type="hidden" name="variable_post_id[]" value="1336"/>
<input type="hidden" name="variable_post_id[]" value="1337"/>
<input type="hidden" name="variable_post_id[]" value="1338"/>

答案 3 :(得分:0)

我认为您的代码没问题,您应该使用form而无需在index中添加form field

<form action="" method="post">
    <input type="hidden" name="variable_post_id[]" value="1336"/>
    <input type="hidden" name="variable_post_id[]" value="1337"/>
    <input type="hidden" name="variable_post_id[]" value="1338"/>
    <input type="submit" calue="submit" name="submit" />
</form>

PHP代码

<?php
    if(isset($_POST['submit']))
    {
        $posts =  $_REQUEST['variable_post_id'];
        foreach ($posts as $post) {
           echo $post;
        }
    }
?>

答案 4 :(得分:0)

如果你想将id保留在括号内,你可以这样做。

<input type="hidden" name="variable_post_id[id][0]" value="1336"/>
<input type="hidden" name="variable_post_id[id][1]" value="1337"/>
<input type="hidden" name="variable_post_id[id][2]" value="1338"/>

$posts =  $_REQUEST['variable_post_id'];
foreach ($posts["id"] as $post) {
   echo $post;

}

或者,如果您想访问特定的一个:

echo $posts["id"][0];