2013-07-29 24 views
2

我有一個多選框,允許我選擇多個數值,然後在點擊箭頭時將它們添加到第二個選擇框。現在這部分工作正常,我可以選擇多個值。但是,當我將第二個選擇框的值傳遞給變量時,變量只顯示列表中最後一項的值,而不是全部。多選框僅顯示最後選定的數值

JavaScript代碼

<script type="text/javascript"> 
    $().ready(function() { 
    $('#add').click(function() { 
    return !$('#select1 option:selected').remove().appendTo('#select2'); 
    }); 
    $('#remove').click(function() { 
    return !$('#select2 option:selected').remove().appendTo('#select1'); 
    }); 
    }); 
    </script> 

HTML代碼

<form action="insert_email_visitor_list1.php" method="get" name="form1" target="_self" id="form1"> 

    <select multiple id="select1" class="mulitiselect" name="select1"> 
    <option value="1">Date/Time</option> 
    <option value="2">Company</option> 
    <option value="3">Location</option> 
    <option value="4">Nof Pages/Visit</option> 
    <option value="5">Traffic Source</option> 
    <option value="6">Search Term</option> 
    <option value="7">Report</option> 
    <option value="8">Classification</option> 
    <option value="9">Owner</option> 
    <option value="10">Phone</option> 
    <option value="11">Town</option> 
    <option value="12">City</option> 
    <option value="12">Country</option> 
    <option value="12">Hostname</option> 
    </select> 


    <a href="#" id="add"><img src="images/add-arrow.png" alt="" /></a> 
     selected Columns 
    <a href="#" id="remove"><img src="images/remove-arrow.png" alt="" /></a> 


    <select multiple id="select2" name="select2" class="mulitiselect"></select> 

    <div class="bottom-img"><img src="images/popup-bottomimg.png" alt="" /></div> 

    <button> 
    <img src="images/info-icon.png" alt="" /> 
    </button> 
    <input type="submit" name="send" id="send" value="Submit" /> 
    </form> 

PHP代碼

$select2 = $_GET['select2']; 
    echo "$select2"; 

基本上我剛過了一些建議,以如果我這個做正確的方法是什麼?

感謝

+0

重複的:http://stackoverflow.com/questions/1227684/how-to -ite-through-multiple-select-options-with-jquery? – Garytje

回答

6

傳遞給PHP領域必須與[],例如命名select2[]。沒有這一點,PHP會將傳入的值視爲單個值,只有傳入的LAST值纔會被放入$ _GET。

在名稱上使用[]會告訴PHP將其視爲多值字段,並且會在$ _GET中創建一個數組,例如,

$_GET['select2'][0] => 'first option selected'; 
$_GET['select2'][1] => 'second option selected'; 

等等

記住multiple參數是一個純粹的客戶端的東西,PHP有沒有辦法知道,這是一個多選的方式。它威利只是看到

select2=foo&select2=bar&select2=baz 

相當於在抵達POST/GET,並選擇在通過最後一個值