How to Get Multiple Selected Values of Select Box in PHP

How to get multiple selected values of select box in php?

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.

I can't get multiple selected values of select box in php

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

How to get multiple selected values of select box in laravel?

It is a typecasting issue in the Model upon calling the create method or Eloquent.

You need to add a variation of the following to your model:

    /**
* Typecast for protection.
*
* @var array
*/
protected $casts = [
'ary' => 'array', // ary being your column name
];

And if you want to see what the request is sending in your method in your controller you can do

$data = $request->all();
dd($data);

That is assuming your method is using the Request class Request $request

Getting All $_POST From Multiple Select Value

I've found the answer...

<select name="cars[]" multiple="multiple">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>

and in the PHP part :

$cars = $_POST['cars'];
print_r ($cars);

How to get all selected values from multiple select option?

Use name as like problems[] instead of problems

//Simple Form and getting the values

<form name="s" method="post" action="" >
<select multiple="multiple" name='problems[]' id='problems' class="inpBox multiple" size='50' style="height:150px;" >
<option value="Cannot Copy">Cannot Copy</option>
<option value="Cannot Print">Cannot Print</option>
<option value="Cannot Print">Cannot Scan</option>
<option value="Cannot Fax">Cannot Fax</option>
<option value="Lines Appear When Printing/Copying">Lines Appear When Printing/Copying</option>
<option value="Scan to Email Failed">Scan to Email Failed</option>
<option value="Toner Low/Empty">Toner Low/Empty</option>
<option value="Others">Others</option>
</select>
<input type="submit" name="submit" value="submit" />
</form>


<?php
if (isset($_POST['submit'])) {
$problems = implode(',', $_POST['problems']);
echo $problems;
}
?>

How to show the selected values from database and show in multiple select box in php

You can loop the select box array and check if it's value is in DB array using in_array()

$a = array("A", "B", "C");

foreach($a as $v)
{
$selected = in_array($v, $db_array) ? 'selected' : '';
}

Get the multiple selected values in the html option with php

Try something like:

$selected = explode(',', $jenis_mesyuarat);
$selected = (in_array($val_user['id'], $selected) ? 'selected="true"' : '')

Note that above is just an example, and it should be further optimized by yourself, like, ensure $jenis_mesyuarat is an array from the beginning (instead of using explode each time you need one).

Insert multiple values from select box to separate rows

A multiple select should have an array style name:

<select name="species[]" multiple size="5">

Then in PHP, $_POST['species'] will be an array containing all the selections. You can loop through the array and insert each of them.

foreach ($species as $s) {
$query_rsCatch = "INSERT INTO SpeciesHunt
(DateCaught, CatchYear, BoatName, BoatMake, Species, Angler, Skipper, Notes, PhotoName, Photo)
VALUES('$datecaught','$year','$boatname','$boatmake','$s','$angler','$skipper','$notes','$imagename','$imagetmp')";
$rsCatch = mysql_query($query_rsCatch, $webdb) or die(mysql_error());
}

You should also stop using the mysql_* functions. They were deprecated many years ago, and finally removed completely in PHP 7. Convert to PDO or mysqli, and also learn to use prepared statements to prevent SQL injection.

Make edit form show selected values in multiple select box it selet the data but double the data

   <?php
$rsmex = explode(",",$data['user']->statesId);
?>
 @foreach($rsmex as $rsmsl)
@foreach($data['state'] as $rsms)
<option @if ( $rsmsl == $rsms->statesId ) {{"selected"}} @endif value={{$rsms->statesId}}>{{$rsms->statesName}}</option>
@endforeach
@endforeach

I'm not sure about the length of the $rsmex.
But if the length of $rsmex is 2, it will loop through all the $data['state'] twice. Then you got those option data twice.

maybe this is what you want to achieve? Just to loop through the state, and see whether user has that statesID?

   @foreach($data['state'] as $rsms)
<option @if (in_array($rsms->statesId, $rsmex)) {{"selected"}} @endif value={{$rsms->statesId}}>{{$rsms->statesName}}</option>
@endforeach



Related Topics



Leave a reply



Submit