I have two multiple selects declared as:
<form method="post"> <input type="text" name="vcgame[]" id="vcgame_1" value=""> <select name="vccoun[][]" id="vccoun_5" multiple="multiple"> <option value="1">West Indies</option> <option value="2" selected="selected">India</option> <option value="3" selected="selected">Australia</option> </select> <input type="text" name="vcgame[]" id="vcgame_2" value=""> <select name="vccoun[][]" id="vccoun_9" multiple="multiple"> <option value="4">Italy</option> <option value="5" selected="selected">Germany</option> </select> </form>
I want to catch the values in PHP, like this
$game_list= $_POST['vcgame']; $country_list= $_POST['vccoun']; $game_country= array(); foreach($game_list as $key=>$val) { $game_country[$key]= $country_list[$key]; }
But the values come like this (with print_r
on the $_POST
):
[vcgame] => Array ( [0] => cricket [1] => football ) [vccoun] => Array ( [0] => Array ( [0] => 2 ) [1] => Array ( [0] => 3 ) [2] => Array ( [0] => 5 ) )
instead of:
[vcgame] => Array ( [0] => cricket [1] => football ) [vccoun] => Array ( [0] => Array ( [0] => 2 [1] => 3 ) [1] => Array ( [0] => 5 ) )
How can this be achieved? Can anyone help? Thanks in advance.
Advertisement
Answer
Try changing the names of the fields on the form to the indices at which you want the desired result:
<form method="post"> <input type="text" name="vcgame[0]" id="vcgame_1" value=""> <select name="vccoun[0][]" id="vccoun_5" multiple="multiple"> <option value="1">West Indies</option> <option value="2" selected="selected">India</option> <option value="3" selected="selected">Australia</option> </select> <input type="text" name="vcgame[1]" id="vcgame_2" value=""> <select name="vccoun[1][]" id="vccoun_9" multiple="multiple"> <option value="4">Italy</option> <option value="5" selected="selected">Germany</option> </select> </form>
Then $_POST
output is:
Array ( [vcgame] => Array ( [0] => cricket [1] => football ) [vccoun] => Array ( [0] => Array ( [0] => 2 [1] => 3 ) [1] => Array ( [0] => 5 ) ) )
Or even like this:
<input type="text" name="data[0][vcgame]" id="vcgame_1" value=""> <select name="data[0][vccoun][]" id="vccoun_5" multiple="multiple"> ... <input type="text" name="data[1][vcgame]" id="vcgame_2" value=""> <select name="data[1][vccoun][]" id="vccoun_9" multiple="multiple">
Array ( [data] => Array ( [0] => Array ( [vcgame] => cricket [vccoun] => Array ( [0] => 2 [1] => 3 ) ) [1] => Array ( [vcgame] => football [vccoun] => Array ( [0] => 5 ) ) ) )