1

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.

1
  • Can you share the code which outputs the first array? (the one you currently have) Commented May 9, 2017 at 10:40

4 Answers 4

1

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);

Sign up to request clarification or add additional context in comments.

Comments

0

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 } ?>

Comments

0

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

Comments

0

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>

Comments

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.