Vigneswaran S
Vigneswaran S

Reputation: 2094

how to make a select option using ajax call .on retrieval it produces an array

how to add the resultant array data as a select option to get dropdown?

 <html>
    <script>
    $(document).ready(function() {
        $("#class_name").change(function(){
            var class_id=$("#class_name").val();
            //alert(""+class_id);
            $.ajax({
                url: "../masters/getsection.php", 
                type: "POST",            
                data:"class_id="+class_id,

                 success: function(data){
                    alert(data);

                }



    });
        });
});

</script>
<body>
<div class="form-group">
            <div class="col-sm-9">
            <select name="class_name" class="form-control" id="class_name">
            <?php
            sizeof($StudentArray);
            for($i=0;$i<sizeof($StudentArray);$i++){
            echo "<option value='".$StudentArray[$i]     ['classId']."'>".$StudentArray[$i]['name']."</option>";
            }
            ?>
            </select>
        </div>
    <div class="form-group">
                <div class="col-sm-9">
                <select  class="form-control" name="section_name" id="section_name" >
                <option value=array['sec_id']>.array['sec_name']</option>//how to give the array value here?

            </select>
        </div>
    </div>

success of data array gives result as

 array([0]=>array(['sec_id']=>1['name']=>A)
 [1]=>array(['sec_id']=>2['name']=>B)
 )

my question is how to print this array value in the secont ? my getsection.php is

<?php
include '../common/inc.common.php';
$class_id=$_POST['class_id'];
$tableName1 = "section";
$fields1="sec_id,sec_name";
$conditions1="where stat='A' and class_id='$class_id'";
 $SectionArray = $Cobj->getCustomData($tableName1,$fields1, $conditions1);
print_r($SectionArray);
?>

Upvotes: 0

Views: 2651

Answers (1)

Payer Ahammed
Payer Ahammed

Reputation: 907

In your getsection.php file remove print_r($SectionArray); line and add

echo json_encode($elements); This statement.

After that modify the

success: function(data){
                    alert(data);

                } 

to

success: function(data){
 for (var i = 0; i < data.length; i++) {
    $('#section_name').append('<option value="' + data[i].sec_id + '">' + data[i].name+ '</option>');
 }
}

Upvotes: 1

Related Questions