deepak singh
asked on
how to fetch data from database to select box and fill it in text box on change dropdown
hii i am creating a select box and a text box with dynamic add button in table and fetching select box value from database. this is my code.
<table class="table table-hover" id="customFields">
<tr>
<th>Test Code</th>
<th>Test Description</th>
</tr>
<tr>
<td>
<select class='form-control code testclas' id="0" name='testcode'>
<option value=''>Select Test Code</option>
<?
$testquery=mysqli_query($d bhandle,"s elect testcode from test where status='active' order by test_type");
if(mysqli_num_rows($testqu ery)>0)
{
while($testrs=mysqli_fetch _array($te stquery)){
$testcode=$testrs['testcod e'];
echo "<option value='$testcode'>$testcod e</option> ";
}
}
?>
</select>
</td>
<td>
<input class="form-control code testdes0" type="text" name="testdesc"/>
</td>
<td>
<a href="javascript:void(0);" class="addCF btn btn-success">Add</a>
</td>
</tr>
</table>
<script type="text/javascript">
$(document).ready(function (){
$(".testclas").change(func tion(){ale rt("ss");
var test_code=$(this).val();
if(test_code=='' )
{ alert("please select Test Code"); return false; }
else
{
var data = 'test_code='+test_code;
var dd=$(this).attr('id');
var id=".testdes"+dd;
alert(id);
$.ajax({
type:'post',
url:'gettest-ajax.php',
data:data,
success: function(data) {
alert(data);
if(data){
$(id).val(data);
}
}
});
}
});
var i=1;
$(".addCF").click(function (){
$.ajax({
url:'gettest-ajax.php',
success: function(data) {
//alert(data);
i++;
if(data!="nodata"){
$("#customFields").append( '<tr><td>< select class="form-control code testclas" id="'+i+'" name="testcode'+i+'"><opti on value="">Select Test Code</option>'+data+'</sel ect></td>< td><input id="testdes'+i+'"class="fo rm-control code testdes" type="text" name="testdesc"/></td><td> <a href="javascript:void(0);" class="remCF btn btn-warning">Remove</a></t d></tr>');
}
}
});
});
});
</script>
please help me.
<table class="table table-hover" id="customFields">
<tr>
<th>Test Code</th>
<th>Test Description</th>
</tr>
<tr>
<td>
<select class='form-control code testclas' id="0" name='testcode'>
<option value=''>Select Test Code</option>
<?
$testquery=mysqli_query($d
if(mysqli_num_rows($testqu
{
while($testrs=mysqli_fetch
$testcode=$testrs['testcod
echo "<option value='$testcode'>$testcod
}
}
?>
</select>
</td>
<td>
<input class="form-control code testdes0" type="text" name="testdesc"/>
</td>
<td>
<a href="javascript:void(0);"
</td>
</tr>
</table>
<script type="text/javascript">
$(document).ready(function
$(".testclas").change(func
var test_code=$(this).val();
if(test_code=='' )
{ alert("please select Test Code"); return false; }
else
{
var data = 'test_code='+test_code;
var dd=$(this).attr('id');
var id=".testdes"+dd;
alert(id);
$.ajax({
type:'post',
url:'gettest-ajax.php',
data:data,
success: function(data) {
alert(data);
if(data){
$(id).val(data);
}
}
});
}
});
var i=1;
$(".addCF").click(function
$.ajax({
url:'gettest-ajax.php',
success: function(data) {
//alert(data);
i++;
if(data!="nodata"){
$("#customFields").append(
}
}
});
});
});
</script>
please help me.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Deepak
Your problem is solved or not if it is solved so please close this question
Thank you
Regards
Mohan SIngh
Your problem is solved or not if it is solved so please close this question
Thank you
Regards
Mohan SIngh
This is working code
I hope it will work for you
Thank You
Regards
Mohan SIngh