Link to home
Start Free TrialLog in
Avatar of deepak singh
deepak singhFlag for India

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($dbhandle,"select testcode from test where status='active' order by test_type");
if(mysqli_num_rows($testquery)>0)
{
while($testrs=mysqli_fetch_array($testquery)){
$testcode=$testrs['testcode'];
echo "<option value='$testcode'>$testcode</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(function(){alert("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+'"><option value="">Select Test Code</option>'+data+'</select></td><td><input id="testdes'+i+'"class="form-control code testdes" type="text" name="testdesc"/></td><td><a href="javascript:void(0);" class="remCF btn btn-warning">Remove</a></td></tr>');
                        }
                  }
            });      
      });
});
</script>
please help me.
Avatar of mohan singh
mohan singh
Flag of India image

https://stackoverflow.com/questions/36596318/select-onchange-update-php-value

I hope it will work for you

Thank You

Regards
Mohan SIngh
ASKER CERTIFIED SOLUTION
Avatar of mohan singh
mohan singh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Deepak

Your problem is solved or not  if it is solved so please close this question

Thank you

Regards
Mohan SIngh
This is working code