Search code examples
htmlselectnames

HTML <select> one name for multiple $_POST variables


What I'm trying to do is to use one select element to get two $_POST variables with the same value. For example:

<select name='a1'>
  <option value='1'>one</option>
  <option value='2'>two</option>
  <option value='3'>three</option>
</select>

will result in $_POST[a1] => 1

what I would like to do is something like this:

<select name='a1, a2'>
  <option value='1'>one</option>
  <option value='2'>two</option>
  <option value='3'>three</option>
</select>

So:

$_POST[a1] => 1, $_POST[a2] => 1

or

$_POST[a1] => 2, $_POST[a2] => 2

whatever the selection might be by the form user.


Solution

  • To avoid an explicit script block (also avoiding jquery dependency), use the following shorthand:

    <!--this should be hidden, set to text Just for preview-->
    <input type="text" name="a2" value="1">
    
    <select name="a1" onchange="document.getElementsByName('a2')[0].value=this.value">
    <option value='1'>one</option>
    <option value='2'>two</option>
    <option value='3'>three</option>
    </select>