I am trying to insert data using ajax in my codeigniter app but every time i click the submit button the whole page is reloads and nothing happens. actually the form submit button is not working in my case. But when i have tried the same code without ajax it can submit the data into database by using action on the form.
My view :
<script type="text/javascript">
/*submit form via ajax*/
$("#createGrade").on('click', function (e) {
e.preventDefault();
$.ajax({
url: '<?php echo base_url();?>index.php?admin/exam_grade/create',
data: {
grade_title: $("#grade_title").val(),
grade_point: $("#grade_point").val(),
mark_from: $("#mark_from").val(),
mark_upto: $("#mark_upto").val(),
comment: $("#comment").val()
},
async: 'true',
cache: 'false',
type: 'post',
success: function (data) {
//jQuery("#attendence_report_holder").html(response);
alert("Data successfully added");
}
});
});
</script>
<form method="post" role="form">
<!-- start form elements -->
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><?php echo get_phrase(' grade_title'); ?></span>
<input type="text" id="grade_title" name="grade_title" autocomplete="off" class="form-control" placeholder="A+" required>
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><?php echo get_phrase(' grade_point'); ?></span>
<input type="number" id="grade_point" name="grade_point" autocomplete="off" class="form-control" placeholder="5" required>
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><?php echo get_phrase(' mark_from'); ?></span>
<input type="number" id="mark_form" name="mark_from" autocomplete="off" class="form-control" placeholder="70" required>
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><?php echo get_phrase(' mark_upto'); ?></span>
<input type="number" id="mark_upto" name="mark_upto" autocomplete="off" class="form-control" placeholder="100" required>
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><?php echo get_phrase(' comment'); ?></span>
<textarea class="form-control" id="comment" rows="3" name="comment" placeholder="add comment"></textarea>
</div>
</div>
<!-- end form elements -->
<div class="col-xs-6 form-group">
<button type="submit" class="btn btn-success pull-left" id="createGrade"><?php echo get_phrase('submit');?></button>
</div>
<div class="col-xs-6 form-group">
<button type="reset" class="btn btn-warning pull-right"><?php echo get_phrase('reset');?></button>
</div>
</form>
and This is my controller:::
function exam_grade($param1 = '', $param2 = '')
{
if ($this->session->userdata('admin_login') != 1)
redirect(base_url(), 'refresh');
if ($param1 == 'create') {
$data['name'] = $this->input->post('grade_title');
$data['grade_point'] = $this->input->post('grade_point');
$data['mark_from'] = $this->input->post('mark_from');
$data['mark_upto'] = $this->input->post('mark_upto');
$data['comment'] = $this->input->post('comment');
$this->db->insert('grade', $data);
$this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));
}
}
document.readyso it is likely running before button exists ... or a script error is being thrown in page and the click handler isn't being activateddocument ready