2011-10-04 2 views
15

저는 PHP를 사용하여 양식을 만들려고 노력하고 있습니다. 필요한 항목에 대한 자습서를 찾은 것 같아서 여기에서 이드가 물어볼 것이라고 생각했습니다. 내 페이지에 여러 확인란 옵션이여러 개의 체크 박스에서 POST 데이터를 가져 옵니까?

...

<li> 
    <label>What service are you enquiring about?</label> 
    <input type="checkbox" value="Static guarding" name="service">Static guarding<br> 
    <input type="checkbox" value="Mobile Patrols" name="service">Mobile Patrols<br> 
    <input type="checkbox" value="Alarm response escorting" name="service">Alarm response escorting<br> 
    <input type="checkbox" value="Alarm response/ Keyholding" name="service">Alarm response/ Keyholding<br> 
    <input type="checkbox" value="Other" name="service">Other<input type="hidden" value="Other" name="service"></span> 
    </li> 

그러나 나는 어떻게 POST 방법을 사용하여 값 모든 체크 박스를 수집 확실하지 않다?

내가

$service = $_POST['service']; 

를 사용하는 경우 난 단지 '다른'다음 배열로 액세스 할 수 있습니다, service[] 대신 service 같은

답변

43

이름을 필드를 반환받을. 그 후, 당신은 배열 정기적 인 기능을 적용 할 수

특정 값이 선택된 경우
  • 확인 :

    echo implode("\n", $_POST['service']); 
    
    :

    if (in_array("Other", $_POST['service'])) { /* Other was selected */} 
    
  • 선택한 모든 옵션을 하나의 개행 문자로 구분 된 문자열을 가져옵니다

  • 모든 체크 박스를 반복합니다.

    (210)
-1
<input type="checkbox" value="Other" name="service">Other<input type="hidden" value="Other" name="service"></span> 

당신은 체크 박스와 같은 이름을 가진 숨겨진 입력 필드를 가지고있다. 이전 필드와 동일한 이름을 가진 "later"필드는 이전 필드의 값을 덮어 씁니다. 즉, 위에 게시 된 양식은 항상 service=Other을 제출하게됩니다.

HTML에서 질문의 표현을 감안할 때, 같은 이름 필드 그룹 중 하나만 선택할 수있는 라디오 버튼을 원하는 것처럼 들립니다. 확인란은 'AND'상황이고 라디오 버튼은 'OR'에 해당합니다.

3

현재 숨겨진 입력을 잡는 중입니다. 왜 그 곳에 숨겨진 입력을 모두 가지고 있습니까? 당신이 "기타"상자를 선택하면 정보를 수집 할 경우, 당신은

<input type="text" name="other" style="diplay:none;"/> 

을 숨길 수 있고 "기타"확인란을 선택하면 당신은 자바 스크립트로 표시 할 수 있습니다. 그런 것.

그냥 ... 이름 속성 서비스는 []

<li> 
<label>What service are you enquiring about?</label> 
<input type="checkbox" value="Static guarding" name="service[]">Static guarding<br> 
<input type="checkbox" value="Mobile Patrols" name="service[]">Mobile Patrols<br> 
<input type="checkbox" value="Alarm response escorting" name="service[]">Alarm response escorting<br> 
<input type="checkbox" value="Alarm response/ Keyholding" name="service[]">Alarm response/ Keyholding<br> 
<input type="checkbox" value="Other" name="service[]">Other</span> 
</li> 

그런 다음 PHP에서 그렇게

$service = $_POST['service']; 
echo $service[0]; // Output will be the value of the first selected checkbox 
echo $service[1]; // Output will be the value of the second selected checkbox 
print_r($service); //Output will be an array of values of the selected checkboxes 

등처럼 액세스 할 수 있도록

관련 문제