0

How can I get all value of the multiple-select and display into the other page. I try to follow different answer from different stackoverflow questions that are related with my question but still they are almost the same giving me wrong output. But correct me if I wrong with that.

I try to analyze and implement answer in the following link that has not display value.
1. Getting All $_POST From Multiple Select Value
2. How to get multiple selected values of select box in php?
3. How to get multiple selected values of select box in php?
4. Get all variables sent with POST?
5. Using $_POST to get select option value from HTML

How can I get the value from my select when I try to submit it using POST or GET? The following codes I am using.

HTML

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
  <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <!-- Bootstrap CSS File  -->
  <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
  <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"
  />
</head>
<body>
  <form id="form1" name="form1" method="GET" action="display.php">
    <div class="row">     
      <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">
        <select name="test" id="sbTwo" multiple="multiple" class="form-control">
          <option value="Alpha">Alpha</option>
          <option value="Beta">Beta</option>
          <option value="Gamma">Gamma</option>
          <option value="Delta">Delta</option>
          <option value="Epsilon">Epsilon</option>
        </select>
      </div>
    </div>    
    <br />
    <input type="submit" class="btn btn-default" name="submit" value="Submit" tabindex="2" />
  </form>
</body>
</html>

<style type="text/css">
  /* Button Primary */
  .btn-primary {
    color: #0275d8;
    background-image: none;
    background-color: transparent;
    border-color: #0275d8;
  }

  .btn-primary:hover {
    background-color: #0275d8;
    color: white;
    border-color: #0275d8;
  }
</style>

PHP

<?php
if (isset($_GET['test'])) {
 foreach ($_GET['test'] as $value){  
    echo $value."\n";
   }
}else{
    echo 'ERROR';
}
?>
Ailyn
  • 265
  • 1
  • 6
  • 17

2 Answers2

3

change the name to test[] for multi select

 <select name="test[]" id="sbTwo" multiple="multiple" class="form-control">
Arun Kumaresh
  • 6,211
  • 6
  • 32
  • 50
0

You have an issue in select box. in multiple select box you need to provide array name . in your case it is test. but it should be test[]

2nd point - if you want to send data using form. you should use POST method because it is secure. so i have replaced GET with POST in your code

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
  <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <!-- Bootstrap CSS File  -->
  <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
  <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"
  />
</head>
<body>
  <form id="form1" name="form1" method="POST" action="display.php">
    <div class="row">     
      <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">
        <select name="test[]" id="sbTwo" multiple="multiple" class="form-control">
          <option value="Alpha">Alpha</option>
          <option value="Beta">Beta</option>
          <option value="Gamma">Gamma</option>
          <option value="Delta">Delta</option>
          <option value="Epsilon">Epsilon</option>
        </select>
      </div>
    </div>    
    <br />
    <input type="submit" class="btn btn-default" name="submit" value="Submit" tabindex="2" />
  </form>
</body>
</html>

<style type="text/css">
  /* Button Primary */
  .btn-primary {
    color: #0275d8;
    background-image: none;
    background-color: transparent;
    border-color: #0275d8;
  }

  .btn-primary:hover {
    background-color: #0275d8;
    color: white;
    border-color: #0275d8;
  }
</style>

and in you display.php  file code should be - 

<?php
if (isset($_POST['test'])) {
 foreach ($_POST['test'] as $value){  
    echo $value."\n";
   }
}else{
    echo 'ERROR';
}
?>
Farsay
  • 312
  • 1
  • 9
  • Nothing happen, still I get the message 'ERROR'. – Ailyn Aug 02 '17 at 07:21
  • i just checked it again and it is working fine. tell me where is your php code. is it in the same page or in a different page 'display.php' – Farsay Aug 02 '17 at 07:25
  • It is located on separate page, display.php – Ailyn Aug 02 '17 at 07:47
  • can you recheck your code ? is it same like i have posted at the bottom section of php code in my answer. if not replace again ... and also check html again is it same like i have posted in my answer... like form method and select box name – Farsay Aug 02 '17 at 07:49
  • It is located on separate page, display.php. I try also to use `var_dump($_GET)` to see what you're getting as a recommendation of Arun Kumaresh. Then I get `array(1) { ["submit"]=> string(6) "Submit" }` – Ailyn Aug 02 '17 at 07:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/150789/discussion-between-ailyn-and-farsay). – Ailyn Aug 02 '17 at 08:02
  • Please help me and talk in https://chat.stackoverflow.com/rooms/150789/discussion-between-ailyn-and-farsay – Ailyn Aug 02 '17 at 08:03