Reputation: 126
I just want to know why is only first value of select tag selected when I click submit button, though I select 2nd or more option?
<div class="form-group my-3">
<select class="form-control" name="role">
<option value="">Select your role</option>
<option value="1" <?=(isset($_SESSION['role'])==1) ? 'selected': ''; unset($_SESSION['role']) ?> >Admin</option>
<option value="2" <?=(isset($_SESSION['role'])==2) ? 'selected': ''; unset($_SESSION['role']) ?>>Modarator</option>
<option value="3" <?=(isset($_SESSION['role'])==3) ? 'selected': ''; unset($_SESSION['role']) ?>>Editor</option>
</select
<!-- error msg if role is not selected -->
<?php if(isset($_SESSION['role_empty'])){ ?>
<div class="alert alert-warning" role="alert">
<?php echo $_SESSION['role_empty']; ?>
</div>
<?php } unset($_SESSION['role_empty']); ?>
</div>
I expect Editor or Modarator, but only I got Admin why?
Upvotes: 3
Views: 256
Reputation: 2322
By default, <select>
appears as a dropdown box, and only allows you to select one <option>
element at a time. This can be overridden by using the multiple
attribute, which tells the browser to render the element as a list box and allow for multiple items to be selected. You can use Ctrl and left-click to select multiple elements manually.
<select class="form-control" name="role" placeholder="Select your role" multiple>
<option value="1" selected>Admin</option>
<option value="2" selected>Moderator</option>
<option value="3">Editor</option>
</select>
Upvotes: 1