I have a 3 column database
1--id 2--project 3--costCentre
I have made the (2) column project as a dropdown option and I would like to populate a text field with the data from the (3) column as dropdown is selected.
The code below is mirroring the dropdown box result in the text field is it an easy tweak.
<script type='text/javascript'>
$(document).ready(function(){
$('#project').val($('#selectdata option:selected').data('project'));
$(function(){
$('#selectdata').change(function(){
$('#softwares').val($('#selectdata option:selected').data('project'));
});
});
});
</script>
</head>
<body>
<div class="container">
<!-- Panel -->
<div class="panel panel-primary">
<div class="panel-heading">Get data from drop down list into textbox</div>
<div class="panel-body">
<!-- Dropdown -->
<div class="form-group">
<label>Softwares List :</label>
<select class="form-control" id="selectdata">
<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "database";
$conn = mysqli_connect($host, $user, $pass, $db);
if(mysqli_connect_errno()){
echo "Failed to connect ".mysqli_connect_error();
}
echo "<option>-Select One-</option>";
$query = mysqli_query($conn, "SELECT * FROM projects") or die (mysqli_error());
while ( $row=mysqli_fetch_assoc($query)) {
echo "<option value='".$row['project']."' data-project='".$row['project']."'>".$row['project']."</option>";
}
?>
</select>
</div>
<!-- Textbox -->
<div class="form-group">
<label>Softwares :</label>
<input type="text" class="form-control" id="softwares">
</div>
Any help would be greatly appreciated.