1

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.

9
  • 1
    What are the values you are expecting? Commented Jan 23, 2021 at 19:06
  • [vccoun]= array(array(2,3), array(5)) Commented Jan 23, 2021 at 19:11
  • Does this answer your question? Submitting a multidimensional array via POST with php Commented Jan 23, 2021 at 19:29
  • checking and let you know Commented Jan 23, 2021 at 19:30
  • Not sure but you may want to do selects like this for a better array structure: name="vccoun[cricket][]" and name="vccoun[football][]". Then you won't need hidden inputs. Commented Jan 23, 2021 at 19:31

1 Answer 1

1

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
                        )
                )
        )
)
Sign up to request clarification or add additional context in comments.

1 Comment

The second alternative is exactly what I wanted. Thanks...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.