2017-02-20 44 views
1

我有具有選擇一個單選按鈕選項5,10,20和50帶變量名稱的輸入字段,如何獲取發佈的值?

我的代碼包含此可能性含名稱從陣列,每個名稱的一種形式:

$names = $names; 
    foreach ($names as $name) 
    {   
     // some div and html coding 
     "<input type='radio' name='".$name[0]."' value='5'>"; 
     "<input type='radio' name='".$name[0]."' value='10'>"; 
     "<input type='radio' name='".$name[0]."' value='20'>"; 
     "<input type='radio' name='".$name[0]."' value='50'>"; 
     // some other html and closing tags 
    } 

的實施例頁面的樣子:

sceenshot

我的問題:

如何我可以獲取發佈的值嗎?

假設name2name4選項10選擇和name5選項20,我如何才能張貼的值到一個數組,看起來像這樣:

$result = array(
    "name2" => "10", 
    "name4" => "10", 
    "name5" => "20" 
); 

非常感謝!

+1

只是刪除從輸入'[0]'指數名稱,如下所示:''「;'然後檢查發佈的數據 –

+1

像'name ='myradio [ 。$ name。「]''然後訪問'print_r($ _ POST ['myradio']);' – JustOnUnderMillions

+0

我喜歡這個,但是然後我只能選擇一個數字而不是每個名字的數字,然後所有的值都成爲同一個單選按鈕的一部分。 – Roddeh

回答

-1
try this.. 
    <php $names = $names;?> 
     <form method="post"> 
     <php 
    foreach ($names as $name) 
     {   
    // some div and html coding 
    "<input type='radio' name='name[".$name."]' value='5'>"; 
    "<input type='radio' name='name[".$name."]' value='10'>"; 
    "<input type='radio' name='name[".$name."]' value='20'>"; 
    "<input type='radio' name='name[".$name."]'' value='50'>"; 
    // some other html and closing tags 
    } ?> 
    <input type="submit" /> 
    </form> 
+0

Sry,但是'$ name []'的返回值是什麼? – JustOnUnderMillions

1

而不是使用的foreach()

你應該循環使用如下

<?php 

if(isset($_POST)){ 
    $names=[1,2,3,4,5,6,7]; 
    foreach ($names as $key => $value) { 
     if(in_array($_POST[$key], $names)) 
     # code... 
      echo 'Do Your Logic Here '; 
    } 
} 
?> 

<form action="s.php" method='post'> 
<?php 

for($i = 0; $i <= count($names) ; $i++){ 
    echo "<input type='radio' name='".$names[$i]."' value='5'>"; 
    echo "<input type='radio' name='".$names[$i]."' value='10'>"; 
    echo "<input type='radio' name='".$names[$i]."' value='20'>"; 
    echo "<input type='radio' name='".$names[$i]."' value='50'>"; 
} 

    echo "<input type='submit' />"; 


?> 
</form> 

這應該解決您的問題

相關問題