2009-09-21 25 views
6
<select id="industryExpect" multiple="multiple"> 
<option value="1">item1</option> 
<option value="2">item2</option> 
<option value="3">item3</option> 
... 
</select> 

sto vedendo qualcosa di simile: industryExpect = 13 & industryExpect = 17 & industryExpect = 19Come gestire più valori di <select> in PHP?

dire che ci sono valori più con la stessa chiave,

Come recuperarli da $ _POST in PHP?

risposta

8

Dategli un nome con [] alla fine, ad esempio:

<select id="industryExpect" name="industryExpect[]" multiple="multiple"> 
    <option value="1">item1</option> 
    <option value="2">item2</option> 
    <option value="3">item3</option> 
</select> 

Poi, nel tuo $_POST dell'array, è ottenere una matrice di tutte le opzioni selezionate:

// assuming that item1 and item3 were selected: 
print_r($_POST['industryExpect']); 
/* 
array 
(
    0 => 1 
    1 => 3 
) 
*/ 
+0

È necessario [] necessario? L'ho incluso e la differenza è solo: industryExpect% 5B% 5D = 6 e industryExpect% 5B% 5D = 11 & industryExpect% 5B% 5D.E% 5B% 5D è [] dopo la codifica. – omg

+0

Oh, funziona, molto strano – omg

+0

Il mio non ha funzionato? –

0

Avvio del file HTML. ex. a.html

<form action="process.php" method="post"> 
    <select name="names[]" multiple="multiple"> 
    <option value="john">john</option> 
    <option value="jack">jack</option> 
    </select> 
    <input type=hidden name=submitted> 
    <input type=submit name=submit> 
</form> 

process.php:

<?php 
print_r($_POST['names']); 
?> 
+0

Provato, non funziona. – omg

+1

Hai provato a farlo funzionare? Prova ora. –

+0

@meder Sei un ghiottone per punizione. – random

Problemi correlati