MojoKing
MojoKing

Reputation: 11

How to get multiple option of select box array in form

I have a form elements in loop and one of them is select box with multiple select option. Here is the code.

<?php for($i = 1; $i <= 2; $i++){ ?>
<input type="text" name="product[]" />
<select name="problems[]"
  <option value="problem1">Problem 1</option>
  <option value="problem2">Problem 2</option>
  <option value="problem3">Problem 3</option>
</select>
<?php } ?>

I enter product "TV ", choose 2 option for product "TV" i.e. problem1 and problem2 and all 3 option for another product Fridge. When i submit the form print the post data, i get data in array

Array
(
    [product] => Array
        (
            [0] => TV
            [1] => Fridge
        )

    [problems] => Array
        (
            [0] => problem1
            [1] => problem2
            [2] => problem1
            [3] => problem1
            [4] => problem3
        )
)

Here, i could not check which problems are of which product. What i want is like this

Array
(
    [product] => Array
        (
            [0] => TV
            [1] => Fridge
        )

    [problems] => Array
        (
            [0] => Array
                (
                    [0] => problem1
                    [1] => problem2
                )
            [1] => Array
                (
                    [0] => problem1
                    [1] => problem2
                    [2] => problem3
                )
        )
)

Is there any way to get array in this way? Your help is highly appreciated.

Upvotes: 1

Views: 1971

Answers (4)

Aftab Ahmad
Aftab Ahmad

Reputation: 344

Correct your code as

<?php for($i = 1; $i <= 2; $i++){ ?>
<input type="text" name="product[<?php echo $i; ?>]" />
<select name="problems[<?php echo $i; ?>][]" multiple>
  <option value="problem1">Problem 1</option>
  <option value="problem2">Problem 2</option>
  <option value="problem3">Problem 3</option>
</select>
<?php } ?>

Also you have error in closing tag i-e <select name="problems[]" close it with closing tag symbol and use multiple keyword like <select name="problems[<?php echo $i; ?>][]" multiple>

Upvotes: 0

Jayesh Raipure
Jayesh Raipure

Reputation: 102

just name your problems field like this <select name="problems[<?php echo $i;?>][]">

Upvotes: 0

Rajdeep Paul
Rajdeep Paul

Reputation: 16963

You need to make use of the counter variable $i in input and select elements, plus you have to make couple of more changes. Refactor your code in the following way,

<?php for($i = 0; $i <= 1; $i++){ ?>
    <input type="text" name="product[<?php echo $i; ?>]" />
    <select name="problems[<?php echo $i; ?>][]" multiple>
        <option value="problem1">Problem 1</option>
        <option value="problem2">Problem 2</option>
        <option value="problem3">Problem 3</option>
    </select>
<?php } ?>

Sidenote: If case you want to see the complete array structure, do var_dump($_POST);

Upvotes: 1

Giacomo M
Giacomo M

Reputation: 4711

What about something like that?

<?php for($i = 1; $i <= 2; $i++){ ?>
<input type="text" name="product_<?php echo $i; ?>[]" />
<select name="problems_<?php echo $i; ?>[]"
    <option value="problem1">Problem 1</option>
    <option value="problem2">Problem 2</option>
    <option value="problem3">Problem 3</option>
</select>
<?php } ?>

Upvotes: 0

Related Questions