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:

<html>
    <head>
    <title>Untitled Document</title>
    </head>
    <body>
    <form id="form1" name="form1" method="get" action="display.php">
      <table width="300" border="1">
        <tr>
          <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>
          </select>
          </td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td><input type="submit" name="Submit" value="Submit" tabindex="2" /></td>
        </tr>
      </table>
    </form>
    </body>
    </html>

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.

 Answers

27

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

<?php
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
 
DMTintner
answered 7 Months ago
22

the code work just right for me:

/home/vagrant/foo/index.php:3:
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:

<?php

var_dump($_POST);

?>
<form method="POST">
     <div class="col-75">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.css" 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>
</select>
<script type="text/javascript">
        var s2 = $("#classe").select2({
    placeholder: "Select",
    tags: true
});


    </script>
</div>
<div class="row">

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

edit:

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
 
LaKaede
answered 7 Months ago
56

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
 
Muazam
answered 7 Months ago
74

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

Wednesday, July 21, 2021
 
Terry
answered 5 Months ago
21

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++){
    selects[i].addEventListener('keydown',function(e){    
        var key = e.which || e.keyCode;
        if(key == 37){
            var previousSibling = this.previousSibling;
            while(previousSibling && previousSibling.nodeType != 1) {
                previousSibling = previousSibling.previousSibling
            }
            previousSibling.focus();
            e.preventDefault();
        }else if(key === 39){
            var nextSibling = this.nextSibling;
            while(nextSibling && nextSibling.nodeType != 1) {
                nextSibling = nextSibling.nextSibling
            }
            nextSibling.focus();
            e.preventDefault();
        }
    })
}

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

Fiddle

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 :
 
Share