Sending Multiple ListBox Selection to PHP

php , xhtml United States
  • 11 years ago
    Hi,
    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>
      </select>

    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?

    Thanks!
















  • 11 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>
      </select>

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

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

    and so forth...

    Hope that helps,


















  • 11 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>
    </select>

    And here is my exact PHP code:

    Apartment:
    $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"];
    $MULTIPLE1 = $POST["MULTIPLE1"];
    $MULTIPLE2 = $POST["MULTIPLE2"];\n\n

    What am I doing wrong? Thanks.








































  • 11 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"

  • 11 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>
    </select>


















    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









  • 10 years ago
    You can find solution here,

    http://www.weberdev.com/get_example-4012.html

    all the best !





  • 8 years ago

    hello people,

    why not used [ ] in your HTML:

    HTML :

        <body>
        <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>
          </select>
          <input type="submit" name="button" id="button" value="Submit" />
        </form>
        </body>
        </html>
    

    PHP :

                <?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).

Contribute

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.

“If Java had true garbage collection, most programs would delete themselves upon execution.” - Robert Sewell