Codeigniter- Page need to load twice to update

I'm trying to insert data to database using ajax, but I need to load the page for me to update the data on my table in my browser. I can see that my pag load after I create or update, but I still need to load my page again to see the result on my table

form.php

<form action="<?php echo site_url($action); ?>" method="post">
    <div class="ibox-content gbItemHolder divider">
        <div class="form-group">
        <label>Disposition Name</label>
            <input type="text" name="disposition_name" id="disposition_name" placeholder="Disposition Name" class="form-control mBottom2 " value="<?php echo $disposition_name; ?>">
            <?php echo "<p>".form_error('disposition_name')."</p>" ?>
         <label>Disposition Code</label>
            <input type="text" name="disposition_code" id="disposition_code" placeholder="Disposition Code" class="form-control mBottom2 " value="<?php echo $disposition_code; ?>"><?php echo "<p>".form_error('disposition_code')."</p>" ?> 

            <label>Disposition Email</label>
            <div id="tags">
            <input type="text" name="email" id="email" placeholder="Enter Email here" class="form-control mBottom2 " value="<?php echo $email; ?>"><!-- <?php echo "<p id='email_validation'>".form_error('disposition_code')."</p>" ?> -->
            </div>
            <input type="text" id="tenant_id" name="tenant_id" value="<?php echo $tenant_id; ?>" hidden="true">
            <input type="hidden" id="calldisp_id"  name="calldisp_id" value="<?php echo $calldisp_id; ?>" />

        </div>
        <div style="margin-top:10px;">
                <input type="hidden" name="calldisp_id" value="<?php echo $calldisp_id; ?>"  /> 
                <button type="submit" class="btn btn-md btn-primary m-t-n-xs" id="triger" value = "<?php echo $button; ?>"><?php echo $button; ?> Call Disposition</button> 

                <a href="<?php echo site_url('admin/call_disposition') ?>" class="btn btn-md btn-warning m-t-n-xs">Cancel</a>

        </div>
    </div>
    </form>

js_files_includes.php

$('#triger').click(function(){
                    var btn_value = $('#triger').val();
                    var tenant_id = $('#tenant_id').val();
                    var calldisp_id = $('#calldisp_id').val();
                    var disposition_name = $('#disposition_name').val();
                    var disposition_code = $('#disposition_code').val();
                    var email = $.map($("#tags span"), function(elem, index){
                         return $(elem).text();
                    }); 
                    var myJsonString = JSON.stringify(email);
                    //alert(myJsonString);
                        if(btn_value == 'Create'){

                            $.ajax({
                                url:"<?php echo base_url(); ?>admin/call_disposition/create_email_dispo_dre",
                                method:"POST",
                                data:{email:myJsonString,
                                      disposition_name:disposition_name,
                                      disposition_code:disposition_code,
                                      tenant_id:tenant_id},
                                dataType: 'json',
                                success:function(data){
                                    toastr.success("Call Disposition Successfully Created");
                                },
                            });
                        }
                        else if(btn_value == 'Update'){
                            $.ajax({
                                url:"<?php echo base_url(); ?>admin/call_disposition/update_email_dispo_dre",
                                method:"POST",
                                data:{email:myJsonString,
                                      disposition_name:disposition_name,
                                      disposition_code:disposition_code,
                                      calldisp_id:calldisp_id,
                                      tenant_id:tenant_id},
                                dataType: 'json',
                                success:function(data){
                                    toastr.success("Call Disposition Successfully Updated");
                                },
                          });
                        }

                });

call_disposition.php

public function create_email_dispo_dre($id){

    $this->_rules();

    if ($this->form_validation->run() == FALSE) {
        $this->update($id);
    } else {
    $data = array(
            'tenant_id' => $this->input->post('tenant_id',TRUE),
            'disposition_code' => $this->input->post('disposition_code',TRUE),
            'disposition_name' => $this->input->post('disposition_name',TRUE),
            'email' => $this->input->post('email',TRUE)           
            );
     $this->calldisp_model->insert($data);
     redirect('admin/call_disposition/update/'.$id);

    }

}
public function update_email_dispo_dre(){

    $data = array(
            'calldisp_id' => $this->input->post('calldisp_id',TRUE),
            'disposition_name' => $this->input->post('disposition_name',TRUE),
            'disposition_code' => $this->input->post('disposition_code',TRUE),
            'email' => $this->input->post('email',TRUE),
            'tenant_id' => $this->input->post('tenant_id',TRUE)
        );
        $this->calldisp_model->update($this->input->post('calldisp_id', TRUE), $data);
        redirect('admin/call_disposition/update/');
}

Answers 1

Related Articles