javascript - Insert Data using ajax on codeigniter -


i trying insert data using ajax in codeigniter app every time click submit button whole page reloads , nothing happens. form submit button not working in case. when have tried same code without ajax can submit data database using action on 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 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 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'));      }  }

@raduation have make working placed code @ end of page or after loading jquery page. alert message displaying .. error message triggered. post data has coding helps trigger error message.

<script>  $("#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 added");      },      error:function(data){        alert("error happend");      }    });  });  })();    </script>


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -