2017-03-15 114 views
0

我想在php代碼中做一個計算器程序。是否可以在一個php表單中從一個文本框輸入多個輸入?

我使用html和javascript製作了它,但我被告知使用php代碼作爲邏輯部分。

有沒有什麼辦法可以從一個文本框,在一個PHP表單中獲取多個輸入?

+0

是的,但你的意思是可取的值的列表從單一textbo x,並把它變成一個數組,或者你的意思是從幾個文本框中取一個數組並將它們變成一個列表? –

+3

可能重複的[如何檢查單個輸入文本框在php中的多個值](http://stackoverflow.com/questions/16052436/how-check-single-input-text-box-for-multiple-values-in- php) – Nawin

+1

,你可以在你的問題中加入一些示例代碼嗎? –

回答

1

是的,您可以輕鬆地在表單中使用多個輸入,方法是給它們不同的名稱並通過$_REQUEST['input_name']訪問它們。

0

在這個例子中,我正在做的是從彈出窗口中選中複選框,並將它們作爲逗號分隔列表放入主窗體的文本輸入字段中。

HTML

<input type="text" id="entry-r1" placeholder="place" tabindex="1"> 
    <a class="show-lookup button" href="#" id="popup-r1" tabindex="2"><i class="fa fa-search"></i></a> 
    <div class="overlay">&nbsp;</div> 
    <div class="lookup-multiselect" id="lookup-r1"> 
     <a class="button close-button right">x</a> 
     <form action="" id="form-r1" name="form-r1" method="post"> 
      <input class="checkall" id="checkall" type="checkbox"> 
     <label for="checkall" class="narrow">Select all</label> 
      <p class="category" id="checkboxes-r1"><strong>Select...</strong><br> 
       <input class="js-popup-focus" type="checkbox" name="place" id="antwerp" value="Antwerp" tabindex="3"> <label for="antwerp">Antwerp</label><br> 
       <input type="checkbox" name="place" id="berlin" value="Berlin" tabindex="3"> <label for="berlin">Berlin</label><br> 
       <input type="checkbox" name="place" id="cairo" value="Cairo" tabindex="3"> <label for="cairo">Cairo</label><br> 
       <input type="checkbox" name="place" id="duss" value="D&uuml;sseldorf" tabindex="3"> <label for="duss">D&uuml;sseldorf</label><br> 
      </p> 
     </form> 
      <a href="#" class="button close-button right" tabindex="4">Use selected</a> 
    </div> 

CSS

.overlay { 
    display: none; 
    position: fixed; 
    text-align: center; 
    top: 0; 
    left: 0; 
    width: 100%; 
    height: 100%; 
    z-index: 1; 
    opacity: 0.7; 
    background: #333; 
} 

.lookup-popup, .lookup-multiselect { 
    padding: 0.5em; 
    display: none; 
    z-index: 99999; 
    background-color: #fff; 
    position: absolute; 
    top: 5em; 
    left: 25%; 
    width: 20%; 
} 

jQuery的

$(document).ready(function() 
{ 

    /* get id of form to work with */ 

    $('.show-lookup').click(function() 
    { 
     var pairedId = $(this).attr('id').split('-'); 
     var lookupToDisplay = '#lookup-' + pairedId[1]; 
     $('.overlay').show(); 
     $(lookupToDisplay).show(); 
     $('.js-popup-focus').focus(); 
    }); 

/* put value selected in lookup into field in main form */ 

    $('.lookup-popup input').on('change', function() 
    { 
     var fieldname = $(this).attr('name'); 
     var pairedId = $(this).parent().attr('id').split('-'); 
     var selOption = $('input[name='+fieldname+']:checked').val(); 
     $("#entry-"+pairedId[1]).val(selOption); 
    }); 

/* for checkbox version, append selected values to field in main form */  

    $('.lookup-multiselect input').on('change', function() 
    { 
     var pairedId = $(this).parent().attr('id').split('-'); 
     //event.preventDefault(); 
     var selOptions = $(".category input:checkbox:checked").map(function(){ 
      return $(this).val(); 
     }).get(); // <---- 
     //console.log(selOptions); 
     var selectedString = selOptions.toString(); 
     $("#entry-"+pairedId[1]).val(selOptions); 
    }); 

    $('.close-button').click(function() 
    { 
     $(this).parent().hide(); 
     var pairedId = $(this).parent().attr('id').split('-'); 
     $('.overlay').hide(); 
     $("#entry-"+pairedId[1]).focus(); 
    }); 
}); 
+0

正如我在我對這個問題的評論中說的,我不是很清楚要問什麼,所以上面可能會變得沒有用 - 但它創建了一種用戶友好的方式,將逗號分隔列表放入單個文本框中,然後可以通過表單提交的PHP頁面將其解析爲數組至 –

相關問題