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.
LVL 1
deepak singhPhp Web Developer Cum DesignerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

I hope it will work for you

Thank You

Regards
Mohan SIngh
0
mohan singhWeb developerCommented:
Page 1  code
<select class='form-control code testclas' id="testcode" name='testcode' onchange="testName(this.value)">
<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> 

<!-- Second select box will show you testcode name -->
<select class='form-control code testclas'  name='testName' id='testName'>  </select>

 
  
<!-- Ajax Script -->

 <script>
    function showBlockLane(id) {
        var reqCode = "<?php echo $project; ?>";
        if (htype == "") {
            document.getElementById("testName").innerHTML = "";
            return;
        } else { 
            if (window.XMLHttpRequest) {
                // code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            } else {
                // code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function() {
                if (this.readyState == 4 && this.status == 200) {
                    document.getElementById("testName").innerHTML = this.responseText;
                }
            };
            xmlhttp.open("GET","ajax_house_scheme.php?c=htype,true);
            xmlhttp.send();
        }
    } 
 </script>


 

Open in new window




Page 2

get requested id which is pass through Ajax page

<?php
    include('config.php');
    if(isset($_GET['c'])){
       
       $c = $_GET['c'];
       $code = $_GET['reqCode'];        
       if($c == 'ROW HOUSE'){
       $fetched_sql =mysql_query($conn, "SELECT * FROM table where id = '$c')";                      
       echo "<select>";
       echo "<option style='font-family: Verdana, Geneva, sans-serif;'>"; echo "Select Scheme"; echo "</option>";
       while($row = mysql_fetch_array($result_states)){ 
            echo"<option style='font-family: Verdana, Geneva, sans-serif;' value='".$row['name']."'>".$row['name']."</option>";
            }
       echo "</select>";        
       } 
       else 
   
    }
 
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mohan singhWeb developerCommented:
Deepak

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

Thank you

Regards
Mohan SIngh
0
mohan singhWeb developerCommented:
This is working code
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.