Asked  7 Months ago    Answers:  5   Viewed   90 times

I have a html form which has a select list box from which you can select multiple values because its multiple property is set to multiple. Consider form method is 'GET'. The html code for the form is as follows:

    <title>Untitled Document</title>
    <form id="form1" name="form1" method="get" action="display.php">
      <table width="300" border="1">
          <td><label>Multiple Selection </label>&nbsp;</td>
          <td><select name="select2" size="3" multiple="multiple" tabindex="1">
            <option value="11">eleven</option>
            <option value="12">twelve</option>
            <option value="13">thirette</option>
            <option value="14">fourteen</option>
            <option value="15">fifteen</option>
            <option value="16">sixteen</option>
            <option value="17">seventeen</option>
            <option value="18">eighteen</option>
            <option value="19">nineteen</option>
            <option value="20">twenty</option>
          <td><input type="submit" name="Submit" value="Submit" tabindex="2" /></td>

I want to display the selected values in select list box on display.php page. So how are the selected values accessed on display.php page using $_GET[] array.



If you want PHP to treat $_GET['select2'] as an array of options just add square brackets to the name of the select element like this: <select name="select2[]" multiple …

Then you can acces the array in your PHP script

header("Content-Type: text/plain");

foreach ($_GET['select2'] as $selectedOption)
    echo $selectedOption."n";

$_GET may be substituted by $_POST depending on the <form method="…" value.

Tuesday, June 1, 2021
answered 7 Months ago

the code work just right for me:

array (size=2)
  'classe' => 
    array (size=2)
      0 => string '| adjective |' (length=13)
      1 => string '| adverb |' (length=10)
  'submitSave' => string 'Save' (length=4)

have some special config in PHP?

my test:



<form method="POST">
     <div class="col-75">
    <script src=""></script>
<script src=""></script>
<link href="" rel="stylesheet"/>
    <select id="classe" name="classe[]" multiple name="event_type[]" class="form-control" required>
  <option value="| adjective |">adjective</option>
  <option value="| adverb |">adverb</option>
  <option value="| noun |">noun</option>
  <option value="| verb |">verb</option>
<script type="text/javascript">
        var s2 = $("#classe").select2({
    placeholder: "Select",
    tags: true

<div class="row">

            <td><input type="submit" value="Save" name="submitSave"></td>


when yo receive array data in PHP, in your case:

<select name="classes[]" >

the PHP var is $_POST['classes'] not $_POST['classes[]'] and it is an array, if you want to use it as an string you have to use implode

Saturday, May 29, 2021
answered 7 Months ago

With the way your string is built, it's a fairly simple str_replace(), which is nice as it saves the hassle of needing regular expressions:

$dropdown = str_replace("value='".$rid."'","value='".$rid."' selected="selected"",$dropdown);
Saturday, May 29, 2021
answered 7 Months ago

request.getParameterValues("select2") returns an array of all submitted values.

Wednesday, July 21, 2021
answered 5 Months ago

Changing default behavior of controls is sometimes frustrating for users. But other times the user expect it works like excel like in your case :)

You can do something like this:

var selects = document.getElementsByTagName('select');
for (var i = 0; i < selects.length; i++){
        var key = e.which || e.keyCode;
        if(key == 37){
            var previousSibling = this.previousSibling;
            while(previousSibling && previousSibling.nodeType != 1) {
                previousSibling = previousSibling.previousSibling
        }else if(key === 39){
            var nextSibling = this.nextSibling;
            while(nextSibling && nextSibling.nodeType != 1) {
                nextSibling = nextSibling.nextSibling

Key 37 = and key 39 is .
e.preventDefault(); prevents the default behaviour of the key you pressed.


Monday, August 30, 2021
Philip Weiser
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :