Skip to content Skip to sidebar Skip to footer

How To Distinguish Between Two Multiple Selection Lists With Same Name

For a system I'm building I need to see which options came in (with POST request) from the first list and which options came in from the second list.

Solution 1:

Divide the categories by creating a multidimensional array in the form:

<form method="post">
    <select name="cars[0][]" multiple>
        <option selected>test</option>
        <option selected>test2</option>
        <option>test3</option>
        <option>test4</option>
    </select>
    <select name="cars[1][]" multiple>
        <option>hai</option>
        <option>hai2</option>
        <option selected>hai3</option>
        <option selected>hai4</option>
    </select>
    <input type="submit">
</form>

And then reading it like this: $_POST['cars'][0] for the first set and $_POST['cars'][1] for the second


Solution 2:

You should be able to retrieve them like this:

$_POST['cars'][0] refers to the "test" set

$_POST['cars'][1] refers to the "hai" set


Solution 3:

I made this:

<?php

    if (isset($_POST['cars']))
    {
      $test = array();
      $hai = array();

      $lista = $_POST['cars'];
      foreach ($lista as $key ) {
          if (substr($key,0,1)=="t")
          {
            $test[] = $key;
          }
          else
          {
            $hai[] = $key;
          }
      }
    }
    var_dump($test);
    var_dump($hai);
    ?>


    <form method="post">
        <select name="cars[]" multiple>
            <option selected>test</option>
            <option selected>test2</option>
            <option>test3</option>
            <option>test4</option>
        </select>
        <select name="cars[]" multiple>
            <option>hai</option>
            <option>hai2</option>
            <option selected>hai3</option>
            <option selected>hai4</option>
        </select>
        <input type="submit">
    </form>

Tested on localhost.

Saludos :)


Post a Comment for "How To Distinguish Between Two Multiple Selection Lists With Same Name"