2010-02-22 158 views
8
<input type="checkbox" class='form' name="checkbox_1" /> 

<input type="checkbox" class='form' name="checkbox_2" /> 

<input type="checkbox" class='form' name="checkbox_3" /> 

......... 

<input type="checkbox" class='form' name="checkbox_10" /> 

from已經使用「POST」方法提交。確定哪些複選框並按遞增順序編寫它們的編號。用空格分隔所有數字(不是新行),不要使用任何HTML格式。如何使用PHP post方法提交複選框值

例如:

如果選中了複選框3,5和10,

輸出繼電器將是:

+0

所以,這裏有什麼問題: 提交表單通過使用下面的代碼獲得選中的複選框的值之後? – anthares 2010-02-22 09:07:30

+0

那麼問題是什麼?詳細說明你想要的東西。 – Kangkan 2010-02-22 09:08:32

+0

有什麼理由在這裏不接受答案嗎? – tw16 2012-03-15 12:41:00

回答

21

更改的標記到像

<input type="checkbox" class='form' value="1" name="checkbox[]" /> 
<input type="checkbox" class='form' value="2" name="checkbox[]" /> 
<input type="checkbox" class='form' value="3" name="checkbox[]" /> 

並得到v alues提交,使用一個簡單的循環

foreach($_POST['checkbox'] as $checkbox){ 
    echo $checkbox . ' '; 
} 
+8

if(isset($ _ POST ['checkbox'])&& is_array($ _ POST ['checkbox'])){ echo implode('',$ _POST ['checkbox']); } – janmoesen 2010-02-22 10:25:41

+0

如何在簡單的HTML表單中使用php變量複選框[]? – 2013-01-02 09:18:11

5

遍歷$_POST陣列和使用preg_match()拉出數如果它與 「checkbox_」 開始:

$checked = array(); 
foreach ($_POST as $k => $v) { 
    if (preg_match('|^checkbox_(\d+)$!', $k, $matches) { 
    $checked[] = $matches[1]; 
    } 
} 
echo implode(' ', $matches); 
+1

vsr說什麼似乎更合適,這樣你就不必使用正則表達式搜索字段。 – azkotoki 2010-02-22 10:25:51

4
<?php 
$checked = array(); 

foreach ($_POST as $k => $v) { 
$subject = $k; 
$pattern = '/^checkbox_(\d+)$/'; 
    if (preg_match($pattern, $subject, $matches)) 
    { 
    $checked[] = $matches[1]; 
    } 
} 

asort($checked); 

foreach ($checked as $key=>$value) 
echo $value .' '; 
?> 
0

對於您可以使用下面的方法來得到複選框值每個複選框的值取出進行檢查或不....

在我的表單頁面(週一至週日)

<input type="checkbox" name="checked[]" class="onoffswitch-checkbox" id="mononoffswitch" value="Mon" checked> 

在我的PHP代碼

function IsChecked($chkname,$value) 
{ 
    if(!empty($_POST[$chkname])) 
    { 
     foreach($_POST[$chkname] as $chkval) 
     { 
      if($chkval == $value) 
      { 
       return true; 
      } 
     } 
    } 
    return false; 
} 

if(IsChecked('checked','Mon')) 
{ 
    $checkedMon = "Y"; 

}else { 
    $checkedMon = "N"; 

} 

if(IsChecked('checked','Tue')) 
{ 
    $checkedTue = "Y"; 

}else { 
    $checkedTue = "N"; 

} 

if(IsChecked('checked','Wed')) 
{ 
    $checkedWed = "Y"; 

}else { 
    $checkedWed = "N"; 

} 

if(IsChecked('checked','Thur')) 
{ 
    $checkedThur = "Y"; 

}else { 
    $checkedThur = "N"; 

} 

if(IsChecked('checked','Fri')) 
{ 
    $checkedFri = "Y"; 

}else { 
    $checkedFri = "N"; 

} 

if(IsChecked('checked','Sat')) 
{ 
    $checkedSat = "Y"; 

}else { 
    $checkedSat = "N"; 

} 

if(IsChecked('checked','Sun')) 
{ 
    $checkedSun = "Y"; 

}else { 
    $checkedSun = "N"; 

} 

現在,你可以得到這些變量的值,並且可以使用您插入進言......

像這樣

$addweekdays = mysqli_query($conn, "INSERT INTO weekdays(id,monday,tuesday,wednesday,thursday,friday,saturday,sunday) VALUES('$Id', '$checkedMon', '$checkedTue', '$checkedWed', '$checkedThur','$checkedFri','$checkedSat','$checkedSun')") ... 
11

HTML代碼::

<input type="checkbox" name="arrayValue[]" value="value1" > value1 <br /> 
    <input type="checkbox" name="arrayValue[]" value="value2" > value2 <br /> 
    <input type="checkbox" name="arrayValue[]" value="value3" > value3 <br /> 
    <input type="checkbox" name="arrayValue[]" value="value4" > value4 <br /> 

PHP代碼::

$checkBoxValue = join(", ", $_POST['arrayValue']); // here first (,) is user-define 
                // means, you can change it whatever 
               // you want, even if it can be (space) or others 

現在,您可以在一個變量中獲得'checkbox'的整個值iable

0
$checked = array(); 

foreach ($_POST as $k => $v) { 
$subject = $k; 
$pattern = '/^checkbox_(\d+)$/'; 
    if (preg_match($pattern, $subject, $matches)) 
    { 
    $checked[] = $matches[1]; 
    } 
} 

asort($checked); 

foreach ($checked as $key=>$value) 
echo $value .' '; 
0

採取的數組,你作爲複選框的name="checkbox[]"名字和你所有的複選框,使用相同的名稱。

<?php 
$chk=""; 
foreach($_POST['checkbox'] as $checkbox) 
{ 
$chk=$chk.$checkbox; 
} 
echo $chk; 
?>