2010-11-19 97 views
0

我有一個窗體有幾個文本區域,然後大約20個輸入字段。

的輸入字段被動態創建,使用一個循環,並從值在我的分貝創建(國家)

我的代碼是:

$options = ''; 
$country_code = ''; 
$query = $DB->query("SELECT country_code, country_id, IF(country_code = ".$country_code."', '', '') AS sel FROM exp_sme_countries WHERE site_id='".$this->settings['site_id']."' ORDER BY country_name ASC"); 
      foreach ($query->result as $row) 
      { 
      $options .= '<label>' . 'Phrase for ' . $this->settings['countries'][$row['country_code']] . '</label>' . '<br />'; 
          $options .= '<input style="width: 100%; height: 5%;" id="country_data" type="text" name="' . $row['country_id'] . '" />' . '<br /><br />'; 
          $options .= '<input type="hidden" name="country_id" id="country_id" value="' . $row['country_id'] . '" />'; 

      } 

此輸出的例子,如:

input style="width: 100%; height: 5%;" id="country_data" type="text" name="68" /> 
input style="width: 100%; height: 5%;" id="country_data" type="text" name="28" /> 

現在我的問題是,我如何獲得這些輸入字段的值?

我看過輸出$ _POST,但這似乎返回我無法真正訪問的數據。

可以用任何方式訪問這些值嗎?

還是需要改變我做事的方式?

感謝

+1

它看起來就像你輸出重複的html id一樣 - 不要這樣做。 – Skilldrick 2010-11-19 16:50:27

回答

0

像上面說的,你有

name="country_id" 

可能喜歡70+次爲好。這將是一個問題。

你可以這樣做:

name="country_id[]" 

,它被張貼後得到PHP中的數組。

另外,你確定你在發佈嗎?好像不是,如果$ _POST爲空..

1

改變你的 '名稱' 字段如下:

name="p_' . $row['country_id'] . '" 

那麼你可以去:

foreach ($_POST as $key => $value) { 
     if (substr($key, 0, 2) == "p_") { 
      $country_id[str_replace("p_", "", $key)] = $value; 
     } 
    } 
+0

這似乎工作。我只需要將其插入到一個循環中,以便將所有內容都放入我的數據庫表中。謝謝! – terrid25 2010-11-19 17:08:59

0

$_POST[68]$_POST[28]將返回你所需要的值。您應該使用字母鍵(country_code)。如果由於某些原因數據庫中的數字標識發生更改,則維護代碼時會遇到問題。 HTML還要求您使用唯一的元素ID來使文檔有效。