Get selected option jquery on change function is not working - javascript

When I am select dropdown option then does'not assign the value of selected option in hidden field.
Please correct Where I am doing wrong. Thanks in advance.
<?php
include("../common/config.php");
$time = time();
$id=$_REQUEST['dr_id'];
$allEst = $db->select(array("*"),PREFIX."obligation_pharmacy","obligation_id IN ($id)");
}
?>
<script type="text/javascript">
function populate(unique,sel){
alert(sel.value);
$("#pharmacy_id"+unique).val(sel.value);
}
</script>
<select name="pharmacy_name[]" class="name" id="pharmacy_name<?php echo $time?>" style="width:180px; font-size:11px;" onchange="populate(<?php echo $time?>,this)">
<option value="">Select Pharmacy Name</option>
<?php
foreach($allEst as $ss)
{if($ss->pharmacy_name!=''){?>
<option value="<?php echo $ss->id;?>"><?php echo $ss->pharmacy_name; ?></option>
<?php }} ?>
</select>
<input type="hidden" name="pharmacy_id[]" id="pharmacy_id<?php echo $time?>" value="">

Using jQuery, usually something like this:
$('#my-select').change(function(){
var value = $(this).val();
$('#my-hidden-input').val(value);
});

$('#pharmacy_name').change(function(){
// get selected option value
var option_val = $( "#pharmacy_name option:selected" ).val();
// populate hidden field
$('#pharmacy_id').val(option_val);
}

Related

How to remove a selected option from another select tag when i have five select with same options and values?

I have this code
<?php for($i=1;$i<=5;$i++){ ?>
<select name="bonus<?php echo $i;?>" style="margin-top:30px;">
<?php foreach($bonusable as $bns) {?>
<option value="<?php echo $bns['type'];?>"><?php echo $bns['name'];?></option>
<?php } ?>
</select>
<?php }?>
This code display the five select and these have same options with the same value , and i need to delete the option if is selected in another select tag.. I have an image that shows what this code displays
please help me, and sorry for my English!
Try this below code
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<div>
<?php for($i=1;$i<=5;$i++){ ?>
<select id="bonus<?php echo $i;?>" name="bonus<?php echo $i;?>" onchange="Process_form('<?php echo $i;?>',this.value)" style="margin-top:30px;" >
<option value="1">1</option>
<option value="2">2</option>
</select>
<?php }?>
</div>
<script>
function Process_form(Id,Value)
{
<?php for($i=1;$i<=5;$i++){ ?>
var i='<?php echo $i; ?>';
if(i!=Id)
{
$("#bonus"+i+" option[value="+Value+"]").attr('disabled','disabled');
}
<?php } ?>
}
</script>
This is the solution for this.. i tried everything but this give me an error when i click submit:
Undefined index: bonus1 ..
<script>
var $select = $("select");
$select.on("change", function() {
var selected = [];
$.each($select, function(index, select) {
if (select.value !== "") { selected.push(select.value); }
});
$("option").prop("disabled", false);
for (var index in selected) { $('option[value="'+selected[index]+'"]').prop("disabled", true); }
});
</script>

set sibling element's value as current element's value

I need to set a checkbox input's value as equal to value of it's sibling select option.
HTML:
<select name="team[]" class="form-control team" id="team">
<option></option>
<?php foreach ($teams as $team): ?>
<?php
$selected = '';
if (isset($edit)):
$selected = (in_array($team->id, $availableTeams)) ? ' selected' : '';
endif;
?>
<option value="<?php echo $team->id; ?>" <?php echo $selected; ?>><?php echo $team->title; ?></option>
<?php endforeach; ?>
</select>
<label>Lead</label>
<input type="checkbox" name="lead_check[]" value="">
Option values in select option are populated dynamically using loop.
You can use this script.
$(document).ready(function(){
$(document).on("click","#team",function(){
$("input[name='lead_check[]'").val($(this).val());
});
$("#team").trigger("click"); //For setting first time value on page load.
});
It will set selected options value to the checkbox on select box click event.
Assuming you want this to run on page load, here is an example of doing so:
$(function() {
$("input[name='lead_check[]']").attr('value', $("select[name='team[]']").val());
});
This will take the value of the selected option from the dropdown, and will set it as the value of the checkbox.

populate 2nd listbox from 1st

I am trying various codes to populate a 2nd listbox (#box2) but to no avail. My latest attempt is that the JS is not showing anything on change so there must be an error in my code, but I cannot see it. I would be grateful if someone could check my code and show me where I have gone wrong. Many thanks
<?php
<select name="box1[]" id="box1" size="7" multiple="multiple" />
<?php
$i = 0;
while ($row = mysql_fetch_array($result))
{
?>
<option value="<?php echo $row["custref"]; ?>">
<?php echo $row["custref"]; ?></option>
<?php
$i++;
}
?>
<select name="box2[]" id="box2" size="7" multiple="multiple" />
<script type="text/javascript">
$('#box1').change(function()
{
var option = $(this).find('option:selected').val();
$('#box2').val(option);
});
</script>
try this code it will append the selected option from Box1 to Box2
<script type="text/javascript">
$('#box1').change(function()
{
$box1_value=$("#box1").val();
$box1_text=$("#box1 option:selected").text();
$("#box2").append('<option value="'+$box1_value+'">'+$box1_text+'</option>');
}
</script>
Find selected, clone, append:
$('#box1').change(function() {
var option = $(this).find('option:selected').clone();
$('#box2').append(option);
});

Insert value select in textbox html

I read the discussion on how to check outstretched by HTML select and add in HTML text input.
Does not work in my case having a select dynamic that takes while loop with the data from DB. Could you help me find a solution?
I want to display the value and the text that is chosen in the select, in the form below in a text field.
<select name="auto" id="auto">
<option value="false">Auto non presente</option>
<?php while ($row = mysqli_fetch_array($result_modello)):; ?>
<option value="<?php echo $row['id_modello']; ?>"><?php echo $row['nome_modello']; ?></option>
<?php endwhile; ?>
</select>
<select name="marca">
<option value="false">Marca non presente</option>
<?php while ($row = mysqli_fetch_array($result_marca)):; ?>
<option value="<?php echo $row['id_marche']; ?>"><?php echo $row['nome_marche']; ?></option>
<?php endwhile; ?>
</select>
Not using jQuery but with plain old regular Javascript something along these lines should do it.
<form>
<select id='auto' name='auto'>
<option value=1>Banana
<option value=2>Apple
<option value=3>Cherry
<option value=4>Pomegranate
<option value=5>Mango
</select>
<div id='results'></div>
</form>
<script type='text/javascript'>
function display(e){
var el=e.target;
var div=document.getElementById('results');
var value=el.options[el.options.selectedIndex].value;
var text=el.options[el.options.selectedIndex].text;
div.innerHTML=value+text;
}
document.getElementById('auto').addEventListener('change',display,false);
</script>
this will work
var e = document.getElementById("auto");
var value = e.options[e.selectedIndex].value;
var text = e.options[e.selectedIndex].text;
Try this :
$("input[type=text]").val($("select[name=marca]").val());
Or
$("input[type=text]").val($("#auto").val());
If you want display both select use this :
var one = $("#auto").val();
var two = $("select[name=marca]").val();
$("input[type=text]").val(one + " , " + two);
I thank you for your attention to my post .
Only in my case being dynamic text field appear in the select all values ​​.
I wished that in the two text boxes in the second form comparissero only two values ​​chosen in the two select .
you can use .val() for value and .text() for getting text.
var text_val = 'val : ' + $("select").val() + 'text : ' +$("select").text();
$("input[type=text]").val(text_val);

how do i get the select value not the option value in php

Hi can some one help me to my problem.
I have a html and php function here all i want to to is to echoed a select value not the option value
here is my code
Ouput: Patrick or any for the name when I select and send the button
<?php if(isset($_REQUEST['send'])){echo $_POST['test'];}?>
<form method="post">
<select name="test"><option value="1">Patrick</option><option value="2">Maria</option><option value="3">Fe</option></select><input type="submit" name="send" value="submit">
</form>
specify the value in option like this "1_Patrick"
<?php if(isset($_REQUEST['send'])){
$value = explode('_',$_POST['test']);
echo $value[1];
}
?>
<form method="post">
<select name="test"><option value="1_Patrick">Patrick</option><option value="2_Maria">Maria</option><option value="3_Fe">Fe</option></select><input type="submit" name="send" value="submit">
</form>
<?php if(isset($_REQUEST['send'])){echo $_POST['test'];}?>
change the value of your option to you want to display
<form method="post">
<select name="test">
<option value="Patrick">Patrick</option>
<option value="Maria">Maria</option>
<option value="Fe">Fe</option>
</select>
<input type="submit" name="send" value="submit">
</form>
//change the $row['id'] to $row['name']
<?php $q = mysql_query("select * from name");?>
while($row = mysql_fetch_assoc($q)){
echo '<option value='.$row['name'].'>'.$row['name'].'</option>' ;
}
// ok you want to used the id in option value.in your submit code get the name for table check bellow code
<?php if(isset($_REQUEST['send'])){$id=$_POST['test'];
$q = mysql_query("select * from name where id=$id");
$row = mysql_fetch_assoc($q);
echo $row['name'];
}
?>
<?php $q = mysql_query("select * from name");
while($row = mysql_fetch_assoc($q)){
echo '<option value='.$row['id'].'>'.$row['name'].'</option>' ;
}?>
The value of the select will be whatever option is selected. If I select Maria, you will see 2 displayed.
<table cellpadding="3" cellspacing="3" align="center" width="50%">
<tr>
<td>
<select name="user" id="user_id" onChange="selectuser()">
<option value="0">Select User</option>
<option value="1">Patrick</option>
<option value="2">Maria</option>
<option value="3">Fe</option>
</select>
</td>
</tr>
</table>
<script language="javascript">
function selectuser()
{
var name = $("#user_id option:selected").text();
alert(name);
}
when you post any form then data contained in the value attribute of the field will get posted.
So you can change the value data to the desired value, like value="Patrik"
As you said that there is purpose behind adding numeric data to value then my suggestion would be add this numeric data to some other custom attribute like data-customid="1" etc and change value="Patrick"
You can add custom attribute in option value.
Refer this link for details.
pure js:
DEMO
document.addEventListener('DOMContentLoaded', function() {
document.querySelector('select').addEventListener('change', function() {
alert(this.options[this.selectedIndex].innerText);
}, false);
}, false);

Categories

Resources