2013-08-02 71 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];