selected options for multi select Jquery in PHP - php

Im using multi select Jquery (http://www.erichynds.com/examples/jquery-ui-multiselect-
widget/demos/) How can I check in PHP (Controller) which option is selected?
This is my code in View:
<select multiple="multiple" size="5">
<?php
foreach ( $this->restservice->getDepartments() as $department ) {
echo '<optgroup label="' . $department->deptName . '">';
foreach ( $this->restservice->getDepartmentsUsers( $department->deptName ) as $user ) {
echo '<option value="' . $user->{'email'} . '">';
echo $user->{'username'};
echo '</option>';
}
}
echo '</optgroup>';
?>
</select>
PS: Im using Codeigniter and I prefer to user foreach and fetch the value of selected options.
Thanks

You need to give your <select> element a name attribute.
<select name="myselect[]" multiple="multiple" size="5">
Then when the form is submitted to a PHP file, the selected options will be in either $_GET['myselect'] or $_POST['myselect'] depending on the form's method attribute.

Give your <select> a name, suffixed by square brakets:
<select multiple="multiple" size="5" name="myselect[]">
Then you can look for $_POST['myselect'] (or $_GET['myselect']) as an array in your PHP script

Related

how to display all user selected "options" in a list

I need the user to select one, two, three or four items from a drop down list and then display them in the "ul". I have the number of the selected item displayed and I need the value to be displayed.
my code
<select name="courses" size="4" multiple>
<option>BMW</option>
<option>Mercedes</option>
<option>Audi</option>
<option>Volvo</option>
</select>
<?php
if(!isset($_POST["courses"])) {
echo 'error';
} else {
echo '<ul><li>' . implode('</li><li>', $_POST["courses"]) . '</li></ul>';
}...
do not judge strictly, I am new to php thanks in advance
Change name="courses" attribute to name="courses[]", then it will be passed to $_POST and treated as an array, not as single value string.
<select name="courses[]" size="4" multiple>
<option>BMW</option>
<option>Mercedes</option>
<option>Audi</option>
<option>Volvo</option>
Making the name attribute "courses" an array would solve the problem

Based on a condition, how to make a dropdown list single select or multiselect?

I had a requirement where in an employee list dropdown given,
if selectMultiple is set, the dropdown should allow multiple selects, and if selectMultiple is not set, it shouldn't.
<select name="employeeList[]" id="employeeList" class="form-
control" multiple="<?=$selectMultiple?>">
<?php
foreach($employeeList as $employee) {
echo "<option value='" . $employee->$employeeId . "'>" .
$employee->employeeName . "</option>";
}
?>
</select>
It's a phtml file, and $selectMultiple is passed from a controller(as in Phalcon). I have tried something like passing, $selectMultiple ="multiple", so the code will look like
<select name="employeeList[]" id="employeeList" class="form-control" multiple="multiple">
and $selectMultiple="" for the single select case.
<select name="employeeList[]" id="employeeList" class="form-control" multiple="">
But the very presence of multiple attribute itself makes the dropdown list elligible for multiselect.
In short, in either cases, it triggers multiselect regardless of the condition. Please help.
You can simply write your code like
You need to pass $selectMultiple = "multiple" or $selectMultiple = ''
<select name="employeeList[]" id="employeeList" class="form-control" <? echo !empty($selectMultiple) ? $selectMultiple : '' ?>>
<?php
foreach($employeeList as $employee) {
echo "<option value='" . $employee->$employeeId . "'>" .
$employee->employeeName . "</option>";
}
?>
I tried introducing a new variable "chooseId" to get rid of the multiple attribute and hence worked!
Modified the select tag to,
<select name="employeeList[]" id="employeeList" class="form-control"
<?php if ($chooseId== 1) { ?> multiple="multiple" <?php } ?>>
<?php
foreach($employeeList as $employee) {
echo "<option value='" . $employee->$employeeId . "'>" .
$employee->employeeName . "</option>";
}
?>
</select>
Now the code recognises the multiple attribute and enables multiselect only when chooseId is 1, otherwise single select works normally.

Remeber Dropdown Selection Choice after Search Laravel

I have a few dropdown box filters , once you click submit it searches based on what you selected. When the results have loaded, the dropdown boxes reset back to how they were originally opposed to remembering what you previously selected.
This is what they look like.
<select id="buyer" name = "buyer" class="form-control" style="width: 100%" data-placeholder="">
<option value="" >Buyer</option>
<?php
foreach($buyer as $key){
echo '<option value=' . $key->LenderName . ">" . $key->LenderName . "</option>";
}
?>
</select>
How do get the dropdown box to remember the previous choice after search.
You need to say what option needs to be selected after submit, in your case during the loop if the value of "for" is equal with the "buyer" value posted, then add
selected
attribute in your :
<select id="buyer" name = "buyer" class="form-control" style="width: 100%" data-placeholder="">
<option value="" >Buyer</option>
<?php
foreach($buyer as $key){
echo '<option value=' . $key->LenderName;
if ($_POST['buyer']==$key->LenderName) {
echo " selected";
}
echo ">" . $key->LenderName . "</option>";
}
?>
</select>

Select list array - prepopulate the choice to the top of the array

I am printing a simple select list out by looping through an associative array which is populated elsewhere.
<select id="choice" name="choice">
<?php
foreach ($this->choice as $key => $value) {
echo "<option name=".$value." value=".$key.">" .$value. "</option>" ;
}
?>
</select>
This select list gives the options available from the array. However, I am trying to pre-populate the list with the selected value, for example if a user is updating their user information, then I would like to set that choice at the top of the list (ideally without the choice appearing further down the list).
I have access to this data in a variable which I have queried:
$this->selected_choice //value
$this->selected_id //key
Now I have been testing some solutions
<select id="choice" name="choice">
<option name="<?php echo $this->selected_choice; ?>" value="<?php echo $this->selected_id;?>">"<?php echo $this->selected_choice; ?></option>
<?php
foreach ($this->choice as $key => $value) {
echo "<option name=".$value." value=".$key.">" .$value. "</option>" ;
}
?>
</select>
This solution does display the users choice on top of the list, but the foreach loop will also display this option as there is no indication that it shouldn't.
This is my question to you guys, how can I do this so the the selected_choice appears at the top of the list, but will then not appear in the foreach array.
I am hoping there is some simple solution to this problem, as it is not so important, but something I come across quite a lot.
Option have no "name". Name is attribute for SELECT tag.
If option is selected, you need to pass "selected" attribute in body of option
Ex: <option value="1" selected>Tramtada</option>
Do it this way:
<select id="choice" name="choice">
<?php
foreach ($this->choice as $key => $value) {
echo "<option <?php if($key == $this->selected_id) {?> selected <?php } ?> value=".$key.">" .$value. "</option>" ;
}
?>
</select>

Multiple Select Box in Wordpress Custom Meta Box Behaves and looks like text input

I am trying to add a select dropdown with multiple="multiple" in a custom meta box, and the select is appearing with fancy styling, is not clickable, and is not the size I am setting:
<select multiple="multiple" size="3" name="location">
<option value="">Please select</option>
<option value="0">All</option>
<?php
foreach(get_terms('town',array('get' => 'all')) as $term)
{
if (!empty($term->name))
{
$str .= "<option value='" . $term->term_id . "'";
$str .= (is_object_in_term($post->ID, "town", $term->name)) ? " selected>" : ">";
$str .= $term->name . "</option>";
}
} echo $str;?></select>
What I get is more like a text input box in appearance, although firebug shows the code is correct for a select box multiple complete with options. Any help gratefully received.
UPDATE :: ADding code of select for Bingjie's request in comments:
<select name="location" size="3" multiple="multiple">
<option value="">Please select</option>
<option value="0">All</option>
<option value="5">Akbuk</option><option value="4">Altinkum</option></select>
add style css.
<select multiple="multiple" size="3" name="location" style="height:200px;">
Hi thanks for you updates. I think the meta box is still custom meta data which is Key-value paired, so it is not allow to select multiple keys. when you retrieve post_meta, you call
get_post_meta($post_id, $key, $single);
so it is not allowed to use multiple keys.
I am not sure, but it is the only reasonable explanation since the select code appears correct.

Categories