About Using Multi Select? - php

Hi I'm working on the code now and the ticket system sending a notification to a user that selects him about.
I want a notification to select more than one user and more than one user.
I tried to use this for Multi Select more than one user selection, again when I say save too, a single user is registering on this can you help me ?
<div class="form-group select-placeholder">
<label for="assigned" class="control-label">
<?php echo _l('ticket_settings_assign_to'); ?>
</label>
<select name="assigned" id="assigned" class="form-control selectpicker" data-live-search="true" data-none-selected-text="<?php echo _l('dropdown_non_selected_tex'); ?>" data-width="100%">
<option value=""><?php echo _l('ticket_settings_none_assigned'); ?></option>
<?php foreach($staff as $member){ ?>
<option value="<?php echo $member['staffid']; ?>" <?php if($member['staffid'] == get_staff_user_id()){echo '';} ?>>
<?php echo $member['firstname'] . ' ' . $member['lastname'] ; ?>
</option>
<?php } ?>
</select>
</div>

If you want to be able to select multiple you have to add the multiple attribute to the box.
<select multiple>
To receive the values in your code as an array you should change the name from name="assigned" to name="assigned[]"
This will send the multi selected data back as an array to PHP or whichever language you are using
<select multiple name="assigned[]" id="assigned" class="form-control selectpicker" data-live-search="true" data-none-selected-text="<?php echo _l('dropdown_non_selected_tex'); ?>" data-width="100%">

You have to put the multiple attribute in your <select> tag.
Note: The javascript there is used to avoid holding the ctrl-key while selecting multiple options.
window.onmousedown = function (e) {
var el = e.target;
if (el.tagName.toLowerCase() == 'option' && el.parentNode.hasAttribute('multiple')) {
e.preventDefault();
// toggle selection
if (el.hasAttribute('selected')) el.removeAttribute('selected');
else el.setAttribute('selected', '');
// hack to correct buggy behavior
var select = el.parentNode.cloneNode(true);
el.parentNode.parentNode.replaceChild(select, el.parentNode);
}
}
<select name="option-list" size="5" multiple>
<option value="option-1">Option</option>
<option value="option-2">Option</option>
<option value="option-3">Option</option>
<option value="option-4">Option</option>
<option value="option-5">Option</option>
<option value="option-6">Option</option>
<option value="option-7">Option</option>
<option value="option-8">Option</option>
<option value="option-9">Option</option>
</select>

I organized the code this way. But I couldn't get a successful result.
<div class="form-group select-placeholder">
<label for="assigned" class="control-label">
<?php echo _l('ticket_settings_assign_to'); ?>
</label>
<select multiple name="assigned[]" id="assigned" class="form-control selectpicker" data-live-search="true" data-none-selected-text="<?php echo _l('dropdown_non_selected_tex'); ?>" data-width="100%">
<option value=""><?php echo _l('ticket_settings_none_assigned'); ?></option>
<?php foreach($staff as $member){ ?>
<option value="<?php echo $member['staffid']; ?>" <?php if($member['staffid'] == get_staff_user_id()){echo '';} ?>>
<?php echo $member['firstname'] . ' ' . $member['lastname'] ; ?>
</option>
<?php } ?>
</select>
</div>

Related

How to create a PHP dropdown list that allows you to select multiple options?

This is the basis for the code I'm using, drawing options from a database but no matter how I alter this code I will only let me select 1 option or return an error.
<select name="sargentid" id="fieldsargentid" class="form-control">
<?php foreach ($sargent as $sargent) { echo "<option value='" . $sargent->getID() .
"'>$sargent</option>"; }?>
</select>
<select name="sargentid" id="fieldsargentid" class="form-control" multiple=multiple>
<?php
foreach($sargent as $sargentKey => $sargentList){
$values = $sargentList['NameofTheRowInTable'];
?>
<options value = <?php echo $values; ?> ><?php echo $values;?></options>
<?php
}
?>
</select>
A drop-down list that allows multiple selections using the multiple attribute:
<select id="animals" name="animal" multiple>
<option value="cat">Cat</option>
<option value="dog">Dog</option>
<option value="mouse">Mouse</option>
<option value="lion">Lion</option>
</select>
Hold down the Ctrl (windows) / Command (Mac) button to select multiple options.
Demo: https://jsfiddle.net/prk6c9q7/
<select name="sargentid" id="fieldsargentid" class="form-control" multiple>
<? foreach($argent as $data){?>
<option value="<?=$data->getID() ?>"><?= $data ?></option>
<?php } ?>
</select>
Try This

select multiple options at a time

<select class="form-control default-select2 " id="group" data-size="10" data-live-search="true" data-style="btn-white" multiple="multiple">
<option value="" disabled="disabled" selected="selected">Select Group</option>
<!-- <option value="1">Admin</option>
<option value="4">User</option> -->
<?php $groups = $objUser->GetGroups(); ?>
<?php foreach($groups as $group){ ?>
<option value="<?php echo $group['id'] ?>"<?php echo ($data[0]['group_id'] == $group['id'] ? "selected='selected'" : "") ?>><?php echo $group['primary_name'] ?></option> <? } ?>
</select>
Now I want user to select multiple option at a time. i searched the whole internet but i couldn't find the answer to fix this issue. i dont know what i am missing.
Your code can actually manage multiple selections. Just hold Ctrl key and click on different options.
Exemple of multiple select : here
When you submit the form, you will be able to get all the selected options into an array.

prevent double datas in select box

Here is my HTML + PHP :
<select name="type" class="form-control form-update-user" id="type" tabindex=1>
<option selected="selected"><?php echo $thirds['type']; ?></option>
<option value="CLIENT">CLIENT</option>
<option value="AFFRETE">AFFRETE</option>
<option value="DEPOT">DEPOT</option>
</select>
I am using JQUERY as Framework for javascript.
My problem is probably simple. But how can I prevent selectbox to have the same data multiple times? For example, if
$third['type'] = "AFFRETE"
Then, I will have one time AFFRETE as selected value, and again this value in my select box. I tried to remove the selected value with ready function in javascript :
<script type="text/javascript">
$(document).ready(function(){
$("#type option:selected").remove();
})
</script>
But then I have another value selected... logical.
Thank you in advance.
You can do this directly in PHP. Check the array value before adding the element as an option:
<select name="type" class="form-control form-update-user" id="type" tabindex=1>
<?php if (!in_array($thirds['type'], array('CLIENT', 'AFFRETE', 'DEPOT'))): ?>
<option selected="selected"><?php echo $thirds['type']; ?></option>
<?php endif; ?>
<option value="CLIENT">CLIENT</option>
<option value="AFFRETE">AFFRETE</option>
<option value="DEPOT">DEPOT</option>
</select>
Or maybe you're looking for:
<?php $options = array('CLIENT', 'AFFRETE', 'DEPOT'); ?>
<select name="type" class="form-control form-update-user" id="type" tabindex=1>
<?php foreach ($options as $option): ?>
<option value="<?php echo $option; ?>"
<?php if ($option === $thirds['type']): ?>
selected="selected"
<?php endif;
><?php echo $option; ?></option>
<?php endforeach; ?>
</select>
Try doing this:
<select name="type" class="form-control form-update-user" id="type" tabindex=1>
<option value="CLIENT" <?php echo $thirds['type']=='CLIENT'?'selected=selected':''; ?>>CLIENT</option>
<option value="AFFRETE" <?php echo $thirds['type']=='AFFRETE'?'selected=selected':''; ?>>AFFRETE</option>
<option value="DEPOT" <?php echo $thirds['type']=='DEPOT'?'selected=selected':''; ?>>DEPOT</option>
</select>
Remember that this is a quick and dirty way to do this. Prefer making a loop creating the options, instead of hardcoding it.

Set a PHP variable as the value of user selected dropdown form

I'm trying to create a form with text fields and dropdowns. With the text fields, I'm using this code:
<label for="name">Name</label>
<input type="text" id="name" name="name" value="<?php echo $user->name;?>" style="width:95%;"/>
The form then checks if the user has filled out the "name" field with this:
$name = $input->get('name', null);
if($name == null)
return false;
This works fine for the multiple text fields that the form uses, but I don't know how to check for the user input on the dropdowns. How can I send the option selected by the user, similar to the way I did it with the text field, so that I can check to make sure the user selected something? Example:
<label for="department">Department</label>
<select name="department" form="quickcontact_frm">
<option value="default">Select </option>
<option value="1"><?php echo $params->get('department1');?></option>
<option value="2"><?php echo $params->get('department2');?></option>
<option value="3"><?php echo $params->get('department3');?></option>
<option value="4"><?php echo $params->get('department4');?></option>
<option value="5"><?php echo $params->get('department5');?></option>
<option value="6"><?php echo $params->get('department6');?></option>
<option value="7"><?php echo $params->get('department7');?></option>
<option value="8"><?php echo $params->get('department8');?></option>
</select>
$department1 = $input->get('department1', null);
$department2 = $input->get('department2', null);
Etc........
This is set up the exact same way as the text fields as far as I know, but doesn't work and seems like a bad way to do it anyways.
if you want to verify and pre-select one option you should do something like this:
<option value="6" <?php if($params->get('department6')==true){ echo 'selected'; } ?>><?php echo $params->get('department6');?></option>
First of all, instead of having 8 lines like that for your departments, you could use a for loop.
<select name="department" form="quickcontact_frm">
<option value="default">Select </option>
<?php for($i=1; $i<=8; $i++): ?>
<option value="<?php echo $i; ?>"><?php echo $params->get('department' . $i);?></option>
<?php endfor; ?>
</select>
Now, inside your for loop, you can add a check if the $input->get is true.
<?php if($input->get('department' . $i, null)) echo 'selected'; ?>
So if you mix both together, the result would be
<select name="department" form="quickcontact_frm">
<option value="default">Select </option>
<?php for($i=1; $i<=8; $i++): ?>
<option value="<?php echo $i; ?>" <?php if($input->get('department' . $i, null)) echo 'selected'; ?>><?php echo $params->get('department' . $i);?></option>
<?php endfor; ?>
</select>
And if you want to a "pre selected" option use "selected" in the option you want:
<select name="department" form="quickcontact_frm">
<option value="default">Select </option>
<option value="1"><?php echo $params->get('department1');?></option>
<option value="2"><?php echo $params->get('department2');?></option>
<option value="3"><?php echo $params->get('department3');?></option>
<option value="4" selected><?php echo $params->get('department4');?></option>
<option value="5"><?php echo $params->get('department5');?></option>
<option value="6"><?php echo $params->get('department6');?></option>
<option value="7"><?php echo $params->get('department7');?></option>
<option value="8"><?php echo $params->get('department8');?></option>
</select>
As explained by W3CSchools.

Previous dropdown value not getting set with set_value in Codeigniter

The following is my HTML code
<div class="col-md-6">
<label for="martial_status">Martial Status</label>
<select name="martial_status" id="martial_status" class="form-control" value="<?php echo set_value('martial_status'); ?>">
<option value="">Select Martial Status</option>
<?php foreach ($martial_status as $status) { ?>
<option value="<?php echo $status['id'] ?>"><?php echo $status['status_name'] ?></option>
<?php } ?>
</select>
Even though i have used the form_validation in controller side, the value is not getting set in drop down once the form fails submitting. How to set the previous value selected in drop down on form failure. Where am I going wrong.
You cannot set the value straightly to select tag. You need to set the selected attribute to option tag which you want it to be get selected. Just like it below....
<div class="col-md-6">
<label for="martial_status">Martial Status</label>
<select name="martial_status" id="martial_status" class="form-control" value="<?php echo set_value('martial_status'); ?>">
<option value="">Select Martial Status</option>
<?php foreach ($martial_status as $status) { ?>
<option value="<?php echo $status['id'] ?>" <?php if ($status['id'] == set_value('martial_status')) echo "selected = 'selected'"?>><?php echo $status['status_name'] ?></option>
<?php } ?>
</select>
On generating the options tag check which option got selected and echo selected = 'selected' in the attribute of that option tag.

Categories