This function how to use switch case - javascript

I am new in PHP and JS. This function use to get only one vales.
In my program dept1 values are 5 and 1st value select onchange in 1, 2nd select onchange in 2, 3rd value select onchange in 3. so any idea to give code for switch case.
<script>
function deptchange()
{
var x = document.getElementById("dept1").value;
document.getElementById("dept2").value = 2;
}
</script>
<input class="form-last-name form-control" id= 'dept1'
onchange="deptchange()" list="dept" value='<?php echo $dept; ?>' name="department"/>
<datalist id="dept">
<option>
<?php
include 'dblayer.php';
$query = mysqli_query($mysqli,"SELECT department FROM department");
while($row=mysqli_fetch_array($query))
{
echo "<option value='". $row['department']."'>".$row['department'] .'</option>';
}
?>
</option>
</datalist>
<input type="hidden" id='dept2' value=' 'class="form-first-name form-control" />

You can use array with push, So after finished your 5 selection all 5 values push to values array.
<script>
var values = [];
function deptchange()
{
values.push(document.getElementById("dept1").value);
}
</script>
To get the stored elements from array.
var firstElement = values[0];

datalist's option should have value attribute. and there is no need to put closing tag. it's different from select's option.
<datalist id="dept">
<?php
include 'dblayer.php';
$query = mysqli_query($mysqli,"SELECT department FROM department");
while($row=mysqli_fetch_array($query)) {
echo "<option value='". $row['department']."'>';
}
?>
</datalist>

Related

On change of textbox value select a value in dropdown list using JavaScript?

I am using given below code in HTML page to change the value of dropdown list which must less than or equal to the entered value in text box.
Javascript-
function abc(){
var dp = document.getElementById("oildate1");
document.getElementById("ocb3").text = dp;
}
HTML and PHP Code
<input type="text" name="oildate1" id="oildate1" onChange="abc()">
<select name="ocb3" id="ocb3">
<?php
include 'connection.php';
$sql = mysqli_query($con,"select Sno,dateoil, replace(dateoil,'-','') as trimmed_date from dieselrates WHERE Transname='$Transname' order by dateoil ASC");
while ($row = mysqli_fetch_array($sql)){
echo "<option value=$row[Sno]>$row[trimmed_date]</option>";
}
?>
</select>

How to set selected value from the database into a dropdownlist / selectbox in Laravel?

I am using laravel 5.2 and javascript. I have an update form which contain a dropdownlist which I call using ajax with function getTugasDetailUpdate . My problem is how can I set the value from the database and make the dropdownlist selected the value from the database that I have chosen before? This below is my code in the controller.
public function getTugasDetailUpdate(Request $request)
{
$update_tugas_id = $request->get("V_ID_PK");
$getDataListPengikut = DB::select("EXEC dbo.GET_KEMENPAR_LIST_PENGIKUT '".$update_tugas_id."'");
$getPengikut2 = DB::select("EXEC dbo.LOV_M_PENGIKUT");
$msg["opt"] ="";
$no=1;
foreach($getDataListPengikut as $dtListPengikut):
$msg["opt"] .= '<tr>
<td><select class="form-control" id="name_'.$dtListPengikut->KODE.'" name="nameupdate[]" data-live-search="true" style="width:100%">
<option value=""> --Silahkan Pilih-- </option>';
foreach ($getPengikut2 as $getPeng){
$msg["opt"] .= '<option value="'.$getPeng->KODE.'">'. $getPeng->DESKRIPSI .'</option>';
}
$msg["opt"] .='</select>
</td>';
$msg["opt"] .= '</tr>';
$no++;
endforeach;
echo json_encode($msg);
}
And how can I get a value from those controller and set it in my javascript? I want to make var optionS = $('#name_3').html(); has an id same like id="name_'.$dtListPengikut->KODE.'" ? those #name_3 is a hardcode, it could be name_4, name_2, etc.
var i=1;
function addMe(e){
var optionS = $('#name_3').html();
i++;
$('#dynamic_field_update').append('<tr id="row'+i+'" class="dynamic-added"><td><select class="form-control" id="name_3" name="nameupdate[]" data-live-search="true" style="width:100%">'+optionS+' </select></td></tr>');
}
You should separate your HTML from your application logic. I suggest you use Laravel Collective package, here is a minimal example:
#foreach($getDataListPengikut as $dtListPengikut) {
Form::select('XXXXXXXX_name', //Unique name for select
['key' => 'value', 'key2' => 'value2'], //Options for this select
null, //Default option (selected)
[
'placeholder' => 'Pick something', //Classes applied to select
'data-live-search' => 'true',
'style' => 'width:100%'
]
);
#endforeach

Get two values from select option at once

How can I have to values at once from select options through JavaScript function?
Here is my code:
<select name="ledger" id="ledger" onchange="setDebit(this.value)" required>
<option value="" >Select</option>
<?php
$ledgerResult = $this->db->query('SELECT * FROM ledger WHERE account_type = "Creditors" ORDER BY name');
$ledgerData = $ledgerResult->result();
for ($c = 0; $c < count($ledgerData); ++$c) {
$ledger_id = $ledgerData[$c]->id;
$ledger_name = $ledgerData[$c]->name;
$ledger_credit = $ledgerData[$c]->credit; ?>
<option value="<?php echo $ledger_id;?>"><?php echo $ledger_name;?></option>
}
</select>
<script>
function setDebit(ele){
document.getElementById("set_debit").value = ele;
}
</script>
I am getting $ledger_id and sending this value through setDebit() to the script. But what I need is to send $ledger_credit. I can do it by setting it as option value instead of $ledger_id; but I also need value of selectas $ledger_id.
How can I set $ledger_id as option value, but send $ledger_credit through setDebit(this.value)?
<option value="<?php echo $ledger_id.":".$ledger_credit;?>"><?php echo $ledger_name;?></option>
function setDebit(ele){
var Value = document.getElementById("ledger").val();
var Parts = Value.split(":");
var LedgerID = Parts[0];
var LedgerCredit = Parts[1];
}
If you are not using jquery.
<option value="<?php echo $ledger_id."||".$ledger_credit; ?><?php echo $ledger_name;?> </option>
<script>
function setDebit(){
var details = document.getElementById("ledger").value;
var allData = details.split("||");
var ledger_id = allData[0];
var ledger_credit = allData[1];
console.log(ledger_id);
console.log(ledger_credit);
}
</script>
You have to set the attribute multiple in the HTML part:
http://www.w3schools.com/tags/att_select_multiple.asp
After that you can have a look at this question for further info:
How to get all selected values of a multiple select box using JavaScript?
You can add an atribute data-credit in yours options
<option value="<?php echo $ledger_id;?>" data-credit="<?php echo $ledger_credit;?>"><?php echo $ledger_name;?></option>
And in the setDebit function:
var ledger_credit = $('option:selected', "#ledger").data('credit');
You must use jquery in this solution

How to get the correct selected value in Select HTML from javascript?

I get users from mysql query. I show this users into table and add a HTML into php file to all users to change a value:
...
do {
echo "<td > <a>".$row["username"]."</a> </td> \n";
echo "<td > <a>".$row["name"]."</a> </td> \n";
echo "<td > <select id='sel'> <option value='admin'>Admin</option> <option value='user'>User</option> </select> </td> \n";
} while ($row = mysql_fetch_array($result));
echo "</tbody></table> \n";
...
How can I get the option selected??
I'm trying get this in javascript but always get the same first value, independet value selected.
function myFunction() {
var e = document.getElementById("sel");
var strUser = e.options[e.selectedIndex].value;
}
Thanks!
Use the jquery .change function.
Fiddle :- https://jsfiddle.net/a2abaL44/1/
<script type="text/javascript">
$("#sel").change(function(){
var selectVal = $(this).val();
})
</script>
https://api.jquery.com/change/
Because your loop will be executed multiple times (once for each user in the database), you will have many <select> elements with the same ID. You cannot have the same ID given to many different elements, and this is why your JavaScript is only returning the value of the first <select>.
If you tell me what you are needing the selected value for, I can update the answer further.
Your code is fine, Change your select to <select id="sel" onchange="myFunction();">
<script type="text/javascript">
function myFunction() {
var e = document.getElementById("sel");
var strUser = e.options[e.selectedIndex].value;
alert(strUser);
}
</script>

Filter select options based on html input with Jquery and javascript

I'm having a problem with filtering HTML select options witch are based html input. For example:
If i write t-shirt in my input i want to see only T-shirts in my select options
My code looks like this...
<input type="text" name="search" id="inputdata" onkeyup="filter()">
<select name="select[]" id="filtersimilar" multiple>
<?php foreach ($all as $row) { ?>
<option value="<?php echo $row->id_product; ?>" itemid="<?php echo $row->name; ?>"><?php echo $row->name; ?></option>
<?php } ?>
</select>
And JS / Jquery code is:
<script>
function filter(){
inp = $('#inputdata').val();
$("#filtersimilar").change(function() {
var options = $(this).data('options').filter('[itemid=' + inp + ']');
$('#select2').html(options);
});
}
</script>
Have you checked jquery.filter documentation? (http://api.jquery.com/filter/) As #Elfentech pointed out, your referring to something that does not exist (options).
I recommend you make all options invisible with "style="display:none;", and when you do the filtering give the filtered options a "display:block;". But still, your filter method looks really out of standard. Check the documentation to understand how filtering works.
Right answer for me was this one
$(function() {
var opts = $('#filtersimilar option').map(function(){
return [[this.value, $(this).text()]];
});
$('#inputdata').keyup(function(){
var rxp = new RegExp($('#inputdata').val(), 'i');
var optlist = $('#filtersimilar').empty();
opts.each(function(){
if (rxp.test(this[1])) {
optlist.append($('<option/>').attr('value', this[0]).text(this[1]));
}
});
});
});

Categories

Resources