2016-02-26 31 views
1

我想做一個下拉菜單有幾個選擇,並選擇「其他」,這將允許用戶輸入自己的價值。HTML,Javascript,PHP的 - 選擇菜單+其他輸入框

我已經這樣做了,但是那得到的已保存的唯一的價值是從文本輸入框中的值,而如果我不選擇其他,而是從菜單中選擇值爲空的值。

<?php $val = $_POST["Room"]; ?> 
// the value here is blank when choosing the drop down menu options. 

Here's我的代碼(相關部分):

JavaScript函數:

<script type="text/javascript"> 
 

 
    function checkSelectedValue(val){ 
 
    var element=document.getElementById('roomnumber'); 
 

 
    if(val=='other'){ 
 
     element.style.display='block';} 
 
    else{ 
 
     element.style.display='none';} 
 
    } 
 
</script>
<p><label>Room number:<br> 
 
    <select name="Room" size=4 onChange='checkSelectedValue(this.value);'> 
 
    <option value="1/201" selected="selected">1/201</option> 
 
    <option value="2/201">2/201</option> 
 
    <option value="3/201">3/201</option> 
 
    <option value="other">Other</option> 
 
    </label></select> 
 
    <input type="text" name="Room" id="roomnumber" style='display:none;'/> </p>
HTML中的下拉菜單:

而PHP:

$val6 = $_POST["Room"]; 
// I continue to write values into a .csv file and such... 

爲什麼它只是拿起那得到由輸入框時,如果選擇其他寫入的值?

在此先感謝!

回答

1

然後在PHP代碼變化

<input type="text" name="Room" id="roomnumber" style='display:none;'/> 

<input type="text" name="Roomnumber" id="roomnumber" style='display:none;'/> 

不喜歡以下

<?php 
     $val = $_POST["Room"]; 
     if($val=='other'){ 
      $val = $_POST["Roomnumber"]; 
     } 
?> 
0

您只需更改您的輸入框名稱,因爲輸入類型中只有一個名稱,lst名稱值會得到此值。這樣

<input type="text" name="Roomno" id="roomnumber" style='display:none;'/> 
相關問題