2012-07-31 17 views
0

我有一個問題,我正在嘗試解決。我在互聯網搜索,但沒有幫助我..我有多組複選框,我想獲得他們的價值觀,並通過他們在分貝。問題是隻有組的第一個複選框才能獲得正確的值。這裏是我的代碼:一組複選框 - 只有第一個更改值

HTML代碼:

<tr> 
     <th>Δημοτικός Κήπος</th> 
     <td align="center"> 
      <span style="display:none;"> 
       <input type="checkbox" name="place1[]" value="1" id="place1_1"/> 
      </span> 
      <img id="Imageplace1_1" 
      src="unchecked.jpg" 
      width="35" 
      height="35" 
      onclick="CheckBoxClicked('place1_1')" 
      style="cursor:pointer;"/> 
     </td> 
     <td align="center"> 
      <span style="display:none;"> 
       <input type="checkbox" name="place1[]" value="2" id="place1_2"/> 
      </span> 
      <img id="Imageplace1_2" 
      src="unchecked.jpg" 
      width="35" 
      height="35" 
      onclick="CheckBoxClicked('place1_2')" 
      style="cursor:pointer;"/> 
     </td> 
     <td align="center"> 
      <span style="display:none;"> 
       <input type="checkbox" name="place1[]" value="3" id="place1_3"/> 
      </span> 
      <img id="Imageplace1_3" 
      src="unchecked.jpg" 
      width="35" 
      height="35" 
      onclick="CheckBoxClicked('place1_3')" 
      style="cursor:pointer;"/> 
     </td> 
     <td colspan="2" align="center"> 
      <span style="display:none;"> 
       <input type="checkbox" name="place1[]" value="4" id="place1_4"/> 
      </span> 
      <img id="Imageplace1_4" 
      src="unchecked.jpg" 
      width="35" 
      height="35" 
      onclick="CheckBoxClicked('place1_4')" 
      style="cursor:pointer;"/> 
     </td> 
    </tr> 

PHP代碼:

if(isset($_POST['answeres'])) { 
     $place1[0] = (@$_POST['place1'][0]=='1')? $_POST['place1'][0]:'0'; 
     $place1[1] = (@$_POST['place1'][1]=='1')? $_POST['place1'][1]:'0'; 
     $place1[2] = (@$_POST['place1'][2]=='1')? $_POST['place1'][2]:'0'; 
     $place1[3] = (@$_POST['place1'][3]=='1')? $_POST['place1'][3]:'0'; 
     echo $place1[0]; //I get 1 if checked 0 if unchecked 
     echo $place1[1]; //I get 0 all the time 
     echo $place1[2]; //I get 0 all the time 
     echo $place1[3]; //I get 0 all the time 
    }     

javascript代碼:

var CheckBoxCheckedImage = new Image(); 
var CheckBoxUncheckedImage = new Image(); 


CheckBoxCheckedImage.src = "checked.jpg"; 
CheckBoxUncheckedImage.src = "unchecked.jpg"; 

function CheckBoxClicked(CheckBoxid) { 

    if(document.getElementById(CheckBoxid).value == "on"){ 
    //if(document.getElementById(CheckBoxid).checked) { 
     document.getElementById(CheckBoxid).checked = false; 
     document.getElementById("Image"+CheckBoxid).src = CheckBoxUncheckedImage.src; 
    } 
    else{ 
     document.getElementById(CheckBoxid).checked = true; 
     document.getElementById("Image"+CheckBoxid).src = CheckBoxCheckedImage.src;  
    } 
} 

誰能幫助?我不出來....

回答

1

2誤區:

1)在你的PHP你檢查複選框的值是1與否,但在你的HTML複選框的值1,2,34。因此,無論是在HTML中將複選框的值都設置爲1,還是在PHP中根據14進行檢查 - 取決於您想要什麼以及如何決定如何解決第二個錯誤。

2)當僅發佈複選框時,這些複選框是$_POST陣列的一部分,已檢查。這意味着$_POST['place1'][0]將是首先檢查複選框,並不一定是具有該名稱的表單中的第一個複選框。您可以通過使用複選框名(name="place1[0]"name="place1[1]"等)按鍵,或者使用不同的值,每個複選框,反對值檢查,而不是對鍵解決這個問題(提示:看in_array()array_values()

其他提示:

  • 使用print_r($_POST);所以看看什麼是真正張貼到PHP腳本 - 使得調試方便很多。
  • 請勿使用@來抑制錯誤/通知:速度很慢。這是錯誤的編碼(應該避免錯誤,通知應該被照顧 - 而不是抑制它們)。
相關問題