0

I have a problem getting the multiple selected values in the option HTML with PHP.

Below is my coding, I just can make 1 value selected in the option for the below coding result:

If $jenis_mesyuarat is single value, I can get the selected value. If $jenis_mesyuarat are multiple value like 3,4,9 , I cannot get the multiple selected value.

<select id="pengguna_sambung" name="pengguna_sambung" title="pengguna" class="form-control" multiple>
<?php
$sql_user = 'SELECT * FROM user where is_active=1';
$row_user = base_mysqli_select($sql_connection, $sql_user, $types, $values);
foreach ($row_user as $val_user) {
$selected = ($jenis_mesyuarat == $val_user['id'] ? 'selected="true"' : '');
echo '<option value="' . $val_user['id'] . '" ' . $selected . '>' . $val_user['name'] . '</option>';
}
?>
</select>

Javascript

$(document).ready(function(){
     $('#pengguna_sambung').multiselect({
      nonSelectedText: 'Pilih ahli-ahli',
      enableFiltering: true,
      enableCaseInsensitiveFiltering: true,
      buttonWidth:'100%'
     });
    });

I have tried $selected = ($jenis_mesyuarat == $val_user['id'] ? 'selected="true"' : ''); this code to define the selected value, just can single value can be selected, multiple value cannot be selected.

Hope someone can guide me on how to solve the problem. Thanks.

  • Does this answer your question? [How to get multiple selected values of select box in php?](https://stackoverflow.com/questions/2407284/how-to-get-multiple-selected-values-of-select-box-in-php) – CBroe Jun 18 '21 at 09:59
  • @CBroe Thanks. But not similar for my problem. –  Jun 18 '21 at 10:00
  • Where does the value `$jenis_mesyuarat` come from, it's not clear? Is it an array, or just a list of comma-separated values in a string? Either way you're going to need to loop through it and examine each item separately – ADyson Jun 18 '21 at 10:00
  • @ADyson just a list of comma-separated values in a string –  Jun 18 '21 at 10:02
  • For example, $jenis_mesyuarat = `1,3,5`; –  Jun 18 '21 at 10:03
  • So what exactly is your problem, _receiving_ the user’s choices in PHP (for that, you would need to use the naming syntax mentioned in the possible duplicate I suggested, unless your `multiselect` jQuery plugin performs any additional magic in that regard (but we don’t know, since you failed to mentioned which plugin exactly that is supposed to be in the first place); or _setting_ the `selected` attribute on the relevant options when you _create_ this select field? – CBroe Jun 18 '21 at 10:03
  • @CBroe setting the selected attribute on the relevant options when you create this select field. For example, $jenis_mesyuarat = 1,3,5; I want select these number in the multiple option –  Jun 18 '21 at 10:05
  • 2
    You need to [explode](https://www.php.net/manual/en/function.explode.php) the string and then loop through the resulting array to check each one separately to see if it matches. – ADyson Jun 18 '21 at 10:05
  • 2
    … or use `in_array` instead of an explicit loop, for that second part. – CBroe Jun 18 '21 at 10:06
  • Thanks. Can show me the sample base on this question? –  Jun 18 '21 at 10:08
  • 2
    In all seriousness, what part of the suggestion did you not understand? It's pretty trivial: read the documentation I provided and then try it - that's the best way to learn. We'll help after that - if you somehow can't manage it, update the question with your attempt and then explain what goes wrong. – ADyson Jun 18 '21 at 10:08
  • You should use multiselect or select2 for this purpose – Shakeel Ahmad Jun 18 '21 at 10:30

1 Answers1

0

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).

Top-Master
  • 7,611
  • 5
  • 39
  • 71