retrieve the selected dropdown value from data base echo the value and it didnot triggered the 2nd dropdown value using ajax. I have a form to create an activities. The dropdown ajax for program and category is working well when creating new activities. But once i try to display the activities that i have created which is edit form and i retrieve all the value, the ajax program and category is not working
Here my two dropdown program and catergory this two is dependent.
Here is button
<li><a style="text-align:left" class="btn waves-effect waves-light" data-toggle="modal" data-target="#<?php echo $rowss['id']; ?>">Edits</a></li>
Here is modal
<div class="modal fade" id="<?php echo $rows["id"]; ?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="form-group">
<label for="programs" class="col-sm-3 control-label">Program</label>
<div class="col-sm-9">
<select name="programs" class="programs form-control" id="programs" onchange="get_category(this);">
<?php
foreach($programs as $row)
{
if($row['tcp_id']==$eveprrm['tcp_id'])
{
?>
<option value="<?php echo $row['tcp_id']; ?>" selected><?php echo $row['tcp_name']; ?></option>
<?php
} else {
?>
<option value="<?php echo $row['tcp_id']; ?>"><?php echo $row['tcp_name']; ?></option>
<?php
}
}
?>
</select>
</div>
</div>
<div class="form-group">
<label for="categorys" class="col-sm-3 control-label">Category</label>
<div class="col-sm-9">
<select name="categorys" class="form-control" id="categorys">
</select>
</div>
</div>
</div>
</div>
</div>
when i fetch and echo the option value for program the category dropdown didnot triggered the javascript for the ajax. Below is my script for ajax.
<script>
function get_category(sel)
{
var tcp_id = sel.options[sel.selectedIndex].value;
if(tcp_id){
$.ajax({
type:'POST',
url:'fetch_data.php',
data:'tcp_id='+tcp_id,
cache: false,
success:function(html)
{
$('#categorys').html(html);
}
});
}
else
{
$('#categorys').html('<option value="">Select category first</option>');\
}
}
</script>
and this is my fetch_data.php code
if(isset($_POST["tcp_id"]) && !empty($_POST["tcp_id"]))
{
$progs = $bdd->prepare("SELECT * FROM category_new WHERE tca_program_id = ".$_POST['tcp_id']);
$progs->execute();
$rows = $progs->fetchAll();
echo '<option value="">Select category first</option>';
foreach($rows as $row)
{
echo '<option value="'.$row['tca_id'].'">'.$row['tca_name'].'</option>';
}
}