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.

sariDon
  • 7,782
  • 2
  • 16
  • 27
  • 1
    What are the values you are expecting? – Wais Kamal Jan 23 '21 at 19:06
  • [vccoun]= array(array(2,3), array(5)) – sariDon Jan 23 '21 at 19:11
  • Does this answer your question? [Submitting a multidimensional array via POST with php](https://stackoverflow.com/questions/2433727/submitting-a-multidimensional-array-via-post-with-php) – ArSeN Jan 23 '21 at 19:29
  • checking and let you know – sariDon Jan 23 '21 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. – AbraCadaver Jan 23 '21 at 19:31
  • Yes. This will do. But if the input type is 'text' (not 'hidden'), what will be the logic? But a good point. I'm editing the question accordingly. – sariDon Jan 23 '21 at 19:36
  • @ArSeN these blocks are generate by ajax calls and each block can be removed (with a button). So no specific indexing is not possible. I need some generic solution. – sariDon Jan 23 '21 at 19:46
  • Making select single (instead of multiple) will work at once. – sariDon Jan 23 '21 at 19:47
  • `no specific indexing is not possible` why not? You can dynamically index it depending on the added sports, e.g. with javascript. – ArSeN Jan 23 '21 at 19:49

1 Answers1

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
                        )
                )
        )
)
id'7238
  • 2,428
  • 1
  • 3
  • 11