I am wondered why its happening.
Actually, I am using dropdown based textbox selection.
whenever my drop-down option value I am using numbers then textbox filled properly based on my drop-down selection.
Although I am using my dropdown value text/string then textbox values cannot be filled when I change my drop-down selection.
I want drop-down option value text/string then textbox value filled.pls, help a lot.
Working Code:-
<select id="sel_depart">
<option value="0">- Select -</option>
<option value="1">too</option>
<option value="2">Stencil</option>
</select>
<input id="sel_user" size="">
Jquery
<script type="text/javascript">
$(document).ready(function(){
$("#sel_depart").change(function(){
var deptid = $(this).val();
$.ajax({
url: 'getUsers2.php',
type: 'post',
data: {depart:deptid},
dataType: 'json',
success:function(response){
var len = response.length;
$("#sel_user").empty();
for( var i = 0; i<len; i++){
var id1234 = response[i]['id123'];
//var name1234 = response[i]['name123'].children('option:selected').text();
var name1234 = response[i]['name123'];
// $("#sel_user").append("<option value='"+id+"'>"+name+"</option>");
// var value = $(this).children('option:selected').text();
$("#sel_user").val(name1234);
// $("#sel_user").val(value);
// $.isNumeric($("#sel_user").val(name1234));
}
}
});
});
});
</script>
getUsers2.php
<?php
$host = "localhost"; /* Host name */
$user = "root"; /* User */
$password = ""; /* Password */
$dbname = "boti_leave_info"; /* Database name */
$con = mysqli_connect($host, $user, $password,$dbname);
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
$departid = $_POST['depart']; // department id
$sql = "SELECT ID,Mobile FROM user_content WHERE Category=".$departid;
$result = mysqli_query($con,$sql);
$users_arr = array();
while( $row = mysqli_fetch_array($result) ){
$userid = $row['ID'];
$name = $row['Mobile'];
$users_arr[] = array("id123" => $userid, "name123" => $name);
}
// encoding array to json format
echo json_encode($users_arr);
?>
The same code is not working when i just change my <option value="too">too</option>
instead of <option value="1">too</option>
listen for the change
event on the sel_depart
and set the value
of the input accordingly :
$(document).ready(function(){
$('#sel_depart').on('change', function(){
//var value = $(this).val(); // uncomment this line to get the value (0, 1, 2 ... )
var value = $(this).children('option:selected').text();
$('#sel_user').val( value );
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="sel_depart">
<option value="0">- Select -</option>
<option value="1">too</option>
<option value="2">Stencil</option>
</select>
<input id="sel_user" size="">
</div>