2013-08-02 70 views
0

我想提取一些隐藏文本字段的值并不太确定如何去做。php回声隐藏的表单字段

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

我有几个领域是这样的:

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

} 

回答

0

尝试像

$posts = $_POST['variable_post_id']; 

,并确保你保持在窗体中这些hiddenfields和你的HTML会像

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

我已经尝试过这一点。只是试了一遍,我得到以下内容:未定义的索引:variable_post_id [] – danyo

+0

曾经看到我的编辑 – Gautam3164

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; 

} 
+0

我不添加索引,它会自动添加通过wordpress – danyo

+0

@danyo从他们(隐藏的字段)呈现..? – Gautam3164

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"/> 
0

我觉得你的代码是好的,你应该使用form没必要在你的form field像添加index

<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; 
     } 
    } 
?> 
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];