Remeber Dropdown Selection Choice after Search Laravel - php

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>

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.

How to post dynamic dropdown value in php

I have using the following code for my dropdown
<select class="form-control" name="device" id="device" onChange="getBrand(this.value);">
<option selected name="device" id="device" value="<?php echo $row['id'] ?>"><?php echo $row['name']; ?></option>
</select>
for form submit, i have used this code
if(isset($_POST['submit'])){
extract($_POST);
$device = $_POST['device'];
}
I want to get selected value in button submit, currently I got the id instead of value.
Thanks in advance.
You have to put $row['name'] as value of your option
<select class="form-control" name="device" id="device" onChange="getBrand(this.value);">
<option selected name="device" id="device" value="<?php echo $row['name'] ?>"><?php echo $row['name']; ?></option>
</select>
If you can't change option value, you could use jQuery:
$("#your_form").submit( function () {
var option_text = $('#device').find(":selected").text();
$("#your_form").append('<input name="selected_option_text" type="hidden" value="' + option_text + '">');
});
This will add a input field with name "selected_option_text" before submit. So you will find it in your receiving script
You can add a data-attribute on your option
for example
data-value = $row['name']
Then get the data-attr in your Javascript/jQuery

selected options for multi select Jquery in 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

Hide options in a select list using jQuery

How can I hide room numbers that is not at Hotel1 and send chosen room number to next page when you click on submit button. As it is now I will be forced to send only the id for chosen hotel because i need to have the value for selHotel options and selroom the same.
I'm using php, html and Sqlite database. hotelrows and roomrows contains all rows of hotel table and rooms table. Can anyone give me an idea of ​​how I could get this to work? Ie when choosing Hotel1 I can just choose room 101 or 102 in selroom for example. (Sorry if my English is bad, hopefully someone understands what I'm asking for.)
Hotels:
id.....Hotel
1......Hotel1
2......Hotel2
Rooms:
hid....Room#
1.......101
1.......102
2.......301
My php/html:
<select id="selHotel" name="selH">
<option value="default" selected> Select hotel</option>
<?php
foreach($hotelrows as $row)
{
echo "<option value='". $row['id'] . "'>". $row['hotelname'] . "</option>";
}
?>
</select>
<select id="selroom" name="selr">
<option value="default" selected>Select room </option>
<?php
foreach($roomrows as $row)
{
echo "<option value='". $row['hid'] . "'>". $row['roomnum'] . "</option>";
}
?>
</select>
jQuery to hide Hotel2 Rooms then i choose Hotel1:
$("#selHotel").change(function(){
var hotelVal = $(this).find("option:selected").val();
$('#selroom option').hide();
$('#selroom option[value='+hotelVal+']').show();
});
Use the roomnum as value, add your hid as class, then use it as filter when selected:
php:
echo "<option class='cls_". $row['hid'] . "' vlaue='".$row['roomnum']."'>". $row['roomnum'] . "</option>";
page
$("#selHotel").change(function(){
var hotelVal = $(this).find("option:selected").val();
$('#selroom option').hide();
$('#selroom .cls_'+hotelVal).show();
$('#selroom .cls_'+hotelVal).first().attr('selected',true);
});

Categories