Reputation: 295
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';
}
?>
Upvotes: 0
Views: 343
Reputation: 312
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';
}
?>
Upvotes: 0
Reputation: 6311
change the name to test[]
for multi select
<select name="test[]" id="sbTwo" multiple="multiple" class="form-control">
Upvotes: 3