This is a somewhat crazy situation for me. Because my AJAX code does not execute without an alert. If alert is disabled, data will not be added to the select box on table data.
Jquery Code
$(document).ready(function () {
x=0;
$(document).on('click', '.add', function () {
var grade = $('#txt-class-search').val();
var subgrade = $('#txt_sub_class_search').val();
var exam_index=$('#exam-index1').val();
var html = '';
html += '<tr class="append-class" >';
html += '<td><select id="id_'+x+'" title="stu_name" name="stu_index[]" class="form-control stu_name" style="height:35px"></select></td>';
html += '<td><input type="text" name="marks[]" class="form-control stu_marks" style="height:35px" /><input type="hidden" name="exam_index" class="exam" value="'+exam_index+'"></td>';
html += '<td style="text-align:center">\n\
<button type="button" name="remove" class="btn btn-danger btn-sm remove"><span class="glyphicon glyphicon-minus"></span></button></td>\n\
</tr>';
$('#item_table').append(html);
$.ajax({
url: "../../../svr/student/exam-stu-name-search.php",
method: "POST",
data: {grade: grade, subgrade: subgrade},
dataType: "text",
success: function (data) {
$('#id_'+x+'').html(data);
//$('#hello').html(data);
}
});
alert();
x++;
});
});
This is the HTML code that is related to jquery. Dynamically rows are added by jquery. First table data contain select box and retrieve data from database using AJAX. It does not need alert at all. But if the alert is removed, AJAX part will not be executed.
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-8">
<form method="post" id="marks-form">
<div class="table-repsonsive">
<span id="error"></span>
<table class="table table-bordered" id="item_table">
<thead>
<tr>
<th style="text-align:center">Student Name</th>
<th style="text-align:center">Marks</th>
<th style="text-align:center"><button type="button" name="add" class="btn btn-success btn-sm add"><span class="glyphicon glyphicon-plus"></span></button></th>
</tr>
</thead>
<tbody class="tbody-class">
</tbody>
</table>
<div align="center">
<input type="submit" name="submit" class="btn btn-info" value="Insert" />
</div>
</div>
</form>
</div>
<div class="col-md-2"></div>
</div>