2014-09-23 1 views
1

체크 표시가되어 있거나 체크되지 않은 체크 상자의 값을 가져 오는 데 문제가 있습니다. 확인란을 선택하면, 그 특정 체크 박스의 값을 얻을 것이다 체크 박스를 선택 해제 때마다 체크되지 않은 체크 박스의 값은 0선택 및 체크되지 않은 체크 박스 배열의 모든 값을 얻고 PHP의 데이터베이스에 저장하는 방법

것이 내 html 코드입니다 :

<table border="0" cellspacing="0" cellpadding="1"> 
     <tbody> 
     <tr style="height: 40px;"> 
      <td style="width: 240px;"><input type="checkbox" name="checkbox[]" value="5"> Barangay business permit </td> 
      <td style="width: 240px;"><input type="checkbox" name="checkbox[]" value="10"> Business plate </td> 
      <td style="width: 240px;"><input type="checkbox" name="checkbox[]" value="20"> Sanitary inspection fee </td> 
     </tr> 
     <tr style="height: 40px;"> 
      <td style="width: 240px;"><input type="checkbox" name="checkbox[]" value="30"> Environmental clearance </td> 
      <td style="width: 240px;"><input type="checkbox" name="checkbox[]" value="40"> Barangay development </td> 
      <td style="width: 240px;"><input type="checkbox" name="checkbox[]" value="50"> building permit </td> 
     </tr> 
     <tr style="height: 40px;"> 
      <td style="width: 240px;"><input type="checkbox" name="checkbox[]" value="60"> Electrical inspection </td> 
      <td style="width: 240px;"><input type="checkbox" name="checkbox[]" value="70"> Mechanical inspection </td> 
      <td style="width: 240px;"><input type="checkbox" name="checkbox[]" value="80"> Plumbing inspection </td> 
     </tr> 
     </tbody> 
</table> 
<div class="" style="margin: auto; padding-top:20px;">Total <input type="text" name="total" style="color: #222222;" readonly/> 
<input type="submit" name="register" value="Register" style="width: 100px; height:50px; margin:auto; color:#222222;"> 
</form> 

PHP 번호 :

$checkbox=$_POST['checkbox']; 

    for($i=0; $i<count($checkbox); $i++) 
    { 
     $num[$i]=$checkbox[$i]; 
    } 

자바 코드

(검사 및 전체 필드 합계를 생성하는 동안 각 체크 박스의 값을 얻을 수있는 기능).

실제 테스트에서 [0, 1, 0, 0, 1, 1, 0, 0, 0] 테이블의 체크 박스 중 일부를 확인했습니다. 참고 : 1은 선택 사항을 나타내고 0은 선택을 취소하여 명확하게 나타냅니다.

I 제출 버튼을 클릭하여 그 값을 표시

는 I이

[0] => 10

[1] => 40

[2] => 50

있어

하지만 같은 값을 얻으려면이

[0] => 0

[1] => 10

[2] => 0

[3] => 0

[4] => 40

[5] => 50

[6] => 0

[7] => 0

[8] => 0

어떻게 그 값을 얻을 수 있습니까? 나는 체크하지 않은 각 값을 0 & 체크 된 값으로 가져 와서 각 배열을 순서대로 데이터베이스에 저장해야합니다. 정말 도움이 필요합니다. 인터넷에서 검색했지만 문제에 대한 올바른 해결책을 얻지 못했습니다. 내 PHP 코드가 잘못되었다고 생각합니다.

데이터베이스 이름 : 응용 프로그램

테이블 : 수수료

열 : ID, 수수료 1, 수수료 2, 수수료 3, 수수료 4, 수수료 5, 수수료 6, ​​수수료 7, 수수료 8, 수수료 9 그것의 모습으로

+0

선택하지 않았습니다. –

답변

1

, 여기에 array_replace을 사용할 수 있습니다.

array_fill()을 통해 기본 구조를 만드십시오 (체크되지 않은 체크 박스는 POST에 포함되지 않으므로). 그런 다음 마침내 array_replace 입력이 기본값입니다.예 :

Demo

<?php 

$default = array_fill(0, 9, 0); 
if(isset($_POST['register'])) { 
    $input = $_POST['checkbox']; 
    $final = array_replace($default, $input); 
    echo '<pre>'; 
    print_r($final); 
} 

?> 
<form method="POST"> 
    <table border="0" cellspacing="0" cellpadding="1"> 
      <tbody> 
      <tr style="height: 40px;"> 
       <td style="width: 240px;"><input type="checkbox" name="checkbox[0]" value="5" class="toggle_checkbox" /> Barangay business permit </td> 
       <td style="width: 240px;"><input type="checkbox" name="checkbox[1]" value="10" class="toggle_checkbox" /> Business plate </td> 
       <td style="width: 240px;"><input type="checkbox" name="checkbox[2]" value="20" class="toggle_checkbox" /> Sanitary inspection fee </td> 
      </tr> 
      <tr style="height: 40px;"> 
       <td style="width: 240px;"><input type="checkbox" name="checkbox[3]" value="30" class="toggle_checkbox" /> Environmental clearance </td> 
       <td style="width: 240px;"><input type="checkbox" name="checkbox[4]" value="40" class="toggle_checkbox" /> Barangay development </td> 
       <td style="width: 240px;"><input type="checkbox" name="checkbox[5]" value="50" class="toggle_checkbox" /> building permit </td> 
      </tr> 
      <tr style="height: 40px;"> 
       <td style="width: 240px;"><input type="checkbox" name="checkbox[6]" value="60" class="toggle_checkbox" /> Electrical inspection </td> 
       <td style="width: 240px;"><input type="checkbox" name="checkbox[7]" value="70" class="toggle_checkbox" /> Mechanical inspection </td> 
       <td style="width: 240px;"><input type="checkbox" name="checkbox[8]" value="80" class="toggle_checkbox" /> Plumbing inspection </td> 
      </tr> 
      </tbody> 
    <input type="submit" name="register" value="Register" style="width: 100px; height:50px; margin:auto; color:#222222;"> 
    </table> 
</form> 

(!) 참고 :. 당신의 JS에서 자신들이 걸리는 경우 대체 정렬 할 예정 있도록 명시 적으로 name 속성에 인덱스를 둘 필요가

그리고 : 이 때문에 지수의 호출 이름을 사용할 수 없기 때문에

대신 classes으로 바꿀 :

,321 0
+0

내 질문을 편집하여 javascript 기능을 추가했습니다. 나는 당신의 코드를 시도했지만 체크 박스를 체크하는 동안 총 값의 자동 생성은 checkbox []가 ​​checkbox []로 바뀌면 자바 스크립트 함수에 영향을 줄 것이므로 작동하지 않는다. – Ejardy

+0

@Ejardy는'name = ""을 가리키는 대신'.change '를'$ ('. toggle_checkbox ')'로 변경합니다. – Ghost

+0

@ Ejardy 나는 개정판을 다시 작성했습니다 – Ghost

1
<input type='hidden' name='checkbox[0]' value='0'/> 
<input type='checkbox' name='checkbox[0]' value='10'/> 

이름이 같기 때문에 php는 확인란 값이있는 경우 기본값을 덮어 씁니다.

0
$("#save").click(function(){ 
var check = []; 
$('.checkBox:checked').each(function() 
{check.push($(this).val());}); 
var count=check.length; 
var data={"Header":"some data","details":[]}; 
var detail={"values":""}; 
for(i=0;i<=count-1;i++){ 
var val=check[i]; 
detail.values=val; 
data.details.push(detail); 
detail={"values":""}; 
} 
$.ajax({ 
url:"data/save", 
data:JSON.stringify(data), 
success:function(){ 
alert("sukses !!"); 
} 
}); 

}); 
관련 문제