Sending Multiple ListBox Selection to PHP

php , xhtml United States
  • 14 years ago
    How do I write the code in PHP to collect the multiple selected data from a listbox in HTML? Here is my code for the listbox:

      <select name="select" size="1" multiple>
        <option value="first">first</option>
        <option value="second">second</option>
        <option value="third">third</option>
        <option value="fourth">fourth</option>
        <option value="fifth">fifth</option>

    Now, the PHP info is going to an email. All other fields in my HTML form end up in the email except the listbox. How do I write the code in PHP?


  • 14 years ago
    You need to set an "id" for each list item, and this id needs to be unique for each one. Hence, your code would become:

    <select name="select" size="1" multiple>
        <option id="first" value="first">first</option>
        <option id="second" value="second">second</option>
        <option id="third" value="third">third</option>
        <option id="fourth" value="fourth">fourth</option>
        <option id="fifth" value="fifth">fifth</option>

    Then, to select that data, you simply use $POST, thus:

    $first = $POST["first"];
    $second = $POST["second"];

    and so forth...

    Hope that helps,

  • 14 years ago

    Hi, thanks for your quick reply. I added the PHP code to my PHP file, but I got this error:

    Parse error: parse error, unexpected '\"', expecting TSTRING or TVARIABLE or TNUMSTRING in accmail2.php on line 9

    Here is my exact HTML code:

    <select name="select" size="5" multiple>
                      <option id="SINGLE1" value="SINGLE1">SINGLE 1</option>
                      <option id="SINGLE2" value="SINGLE2">SINGLE 2</option>
                      <option id="SINGLE3" value="SINGLE3">SINGLE 3</option>
                      <option id="DOUBLE1" value="DOUBLE1">DOUBLE 1</option>
                      <option id="DOUBLE2" value="DOUBLE2">DOUBLE 2</option>
                      <option id="DOUBLE3" value="DOUBLE3">DOUBLE 3</option>
                      <option id="DOUBLE4" value="DOUBLE4">DOUBLE 4</option>
                      <option id="DOUBLE5" value="DOUBLE5">DOUBLE 5</option>
                      <option id="TRIPLE1" value="TRIPLE1">TRIPLE 1</option>
                      <option id="TRIPLE2" value="TRIPLE2">TRIPLE 2</option>
                      <option id="TRIPLE3" value="TRIPLE3">TRIPLE 3</option>
                      <option id="MULTIPLE1" value="MULTIPLE1">MULTIPLE 1</option>
                      <option id="MULTIPLE2" value="MULTIPLE2">MULTIPLE 2</option>

    And here is my exact PHP code:

    $SINGLE1 = $POST["SINGLE1"];
    $SINGLE2 = $POST["SINGLE2"];
    $SINGLE3 = $POST["SINGLE3"];
    $DOUBLE1 = $POST["DOUBLE1"];
    $DOUBLE2 = $POST["DOUBLE2"];
    $DOUBLE3 = $POST["DOUBLE3"];
    $DOUBLE4 = $POST["DOUBLE4"];
    $DOUBLE5 = $POST["DOUBLE5"];
    $TRIPLE1 = $POST["TRIPLE1"];
    $TRIPLE2 = $POST["TRIPLE2"];
    $TRIPLE3 = $POST["TRIPLE3"];
    $MULTIPLE2 = $POST["MULTIPLE2"];\n\n

    What am I doing wrong? Thanks.

  • 14 years ago

    Something you should note is that you can't any longer just put "multiple" in your tag, as that's invalid XHTML - all attributes and properties must be correctly qualified.

    Therefore you have to do this: multiple="multiple"

  • 14 years ago

    Sorry, I think what I told you before was wrong. You need to add an "id" tag to the form, not to each individual entry. Then, when you post the form, you use $POST["FormName"] to get that information back. So, using your code (modified slightly though):

    <form id="MyForm" name="MyForm" method="post" action="accmail2.php">
    <label>Please select one of the following options:</label>
    <select name="select" size="5" multiple="multiple">
    <option value="SINGLE1">SINGLE 1</option>
    <option value="SINGLE2">SINGLE 2</option>
    <option value="SINGLE3">SINGLE 3</option>
    <option value="DOUBLE1">DOUBLE 1</option>
    <option value="DOUBLE2">DOUBLE 2</option>
    <option value="DOUBLE3">DOUBLE 3</option>
    <option value="DOUBLE4">DOUBLE 4</option>
    <option value="DOUBLE5">DOUBLE 5</option>
    <option value="TRIPLE1">TRIPLE 1</option>
    <option value="TRIPLE2">TRIPLE 2</option>
    <option value="TRIPLE3">TRIPLE 3</option>
    <option value="MULTIPLE1">MULTIPLE 1</option>
    <option value="MULTIPLE2">MULTIPLE 2</option>

    If the user selects, for example "Double 5", then setting

    $MyFormListVariable = $POST["MyForm"];
    should set $MyFormListVariable to "Double5"

    You can of course then go on to use value checking:

    If $MyFormListVariable == "DOUBLE5" THEN
        { ...
        } ELSE {

    type of thing

  • 13 years ago
    You can find solution here,

    all the best !

  • 11 years ago

    hello people,

    why not used [ ] in your HTML:

    HTML :

        <form id="form1" name="form1" method="post" action="">
          <select name="select[]" size="10" multiple="multiple" id="select[]">
            <option value="1">2313123</option>
            <option value="2">53534534</option>
            <option value="3">523523523</option>
            <option value="4">44234234</option>
          <input type="submit" name="button" id="button" value="Submit" />

    PHP :

                if (isset($_POST['select'])) {
                      $az = $_POST['select'];
                      foreach ($az as $a) 
                         echo $a;

Post a reply

Enter your message below

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Computer science education cannot make anybody an expert programmer any more than studying brushes and pigment can make somebody an expert painter” - Eric Raymond