Home  >  Q&A  >  body text

How to list all "options" selected by the user

I need the user to select one, two, three or four items from a dropdown and then display them in the "ul". I have displayed the number of the selected item and I need to display the value.

My code

<select name="courses" size="4" multiple>
        <option>BMW</option>
        <option>Mercedes</option>
        <option>Audi</option>
        <option>Volvo</option>
 </select>

 <?php
 if(!isset($_POST["courses"])) {
     echo 'error';
 } else {
     echo '<ul><li>' . implode('</li><li>', $_POST["courses"]) . '</li></ul>';
 }...

Please don't judge strictly, I'm new to php, thanks in advance.

P粉436410586P粉436410586427 days ago499

reply all(2)I'll reply

  • P粉293550575

    P粉2935505752023-09-12 13:16:49

    <select name="courses[]" size="4" multiple>
        <option>BMW</option>
        <option>Mercedes</option>
        <option>Audi</option>
        <option>Volvo</option>

    Set the name attribute to the array form of "courses" to solve the problem

    reply
    0
  • P粉752812853

    P粉7528128532023-09-12 10:36:48

    Change the name="courses" attribute to name="courses[]", then it will be passed to $_POST and treated as An array, not a single-valued string.

    reply
    0
  • Cancelreply