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?
 
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
 
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:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.