2011-03-21 121 views
0

我有一個有問題的表單,答案是複選框,最後一個答案中有另一個請指定選項,當我檢查該複選框時,文本框使我能寫入它。將文本框的值傳遞給複選框值

我的答案保存在一個數組中。但現在什麼問題是當我提交表單時,我想要另一個的值請指定選項是當選項被選中時填充的文本框的值。 這裏,我有以下形式:

<table>  
<tr> 
     <td colspan="3" style="border-bottom:none">d.&nbsp;If you checked the boxes " very poor or poor clients", which reference point/benchmark do you use for estimating the poverty level of your clients? (Check all that apply):  </td> 
     <td> 
      <input type="checkbox" name="sdd1[]" value="Persons in the bottom 50% of those living below the poverty line established by the national government"/>Persons in the bottom 50% of those living below the poverty line established by the national government <br /> 
      <input type="checkbox" name="sdd1[]" value="Person living on less than US $1 a day international poverty line"/>Person living on less than US $1 a day international poverty line<br /> 
      <input type="checkbox" name="sdd1[]" id="check" onclick="disableMe('mfi_1_d_i_a',this)" value="mfi_1_d_i_a" />Other(please specify): 
      <input type="text" name="mfi_1_d_i_a" class="init" id="mfi_1_d_i_a" maxlength="30" disabled="disabled"/>  </td> 
</tr> 
</table> 

回答

2

實際上,你可以利用一些輸入名稱的功能。

  • 如果輸入被禁用,則輸入將不會出現在$_POST中。
  • 如果沒有名稱,輸入將不會出現在$_POST中。

所以在你的情況下,如果你把名字從「其他」複選框。並將文本框上的名稱設置爲sdd1[],然後所有內容都應按照您的要求工作。

什麼實際發生的是「其他」複選框永遠不會被髮送,而是因爲它啓用時,它被選中的文本框,然後將文本框的值將僅在其他被選中...發送

。希望說得通。


這就是說,你的腳本現在依賴於JavaScript,這並不總是被推薦。如果用戶轉到JavaScript網站禁用的網站,則他們無法在其他文本框中輸入值。

更通用的解決方案是加載啓用了文本框的頁面,並使用Javascript禁用它。因此,如果用戶沒有Javascript,他們仍然可以在文本框中輸入文本,因爲它仍然會被啓用。如果他們有Javascript,當其他複選框被選中/取消選中時,您可以使用禁用/啓用Javascript事件。

然後在PHP中,檢查sdd1數組中的「mfi_1_d_i_a」值,如果存在,請閱讀文本框的值。

0

嗯,我想你可以通過將你的value存儲在數據庫或服務器腳本而不是複選框來解決這個問題。

<input type="checkbox" name="sdd1[]" value="1"/>Value 1 <br/> 
<input type="checkbox" name="sdd1[]" value="2"/>value 2 <br/> 
<input type="checkbox" name="sdd1[]" id="check" onclick="disableMe('mfi_1_d_i_a',this)" value="1"/>Other(please specify): 

<input type="text" name="mfi_1_d_i_a" class="init" id="mfi_1_d_i_a" maxlength="30" disabled="disabled"/> 

而且在PHP腳本

switch($_POST['sdd1']){ 
    case 1: 
     //store value1; 
     break; 
    case 2: 
     //store value2; 
     break; 
    case 3: 
     //store text value 
     break; 
    default: 
     //store some value; 
     break;        
}