How to do update via ajax + php

2

I want to update data through a modal, however it is not updating. Anyone have any idea why you're not updating?

Modal:

<script type="text/javascript">
$(document).ready(function() {
    $('#salvar').click(function() {
        var destino = "<?php echo base_url('protocolo/atualizarprotocolo') ?>";
        var dados= {
            'area': $("#area").val(),
            'status': $('#status').val()
        };
        alert("Protocolo encaminhado com sucesso!");
        location.reload();
        $.ajax({
            type: "POST",
            url: destino,
            data: dados,
     });
      return false;
    });
});

  <?php
  $id = $this->uri->segment(3);
  if ($id > 0) {
  $querys = $this->protocolo->get_protocol_hist($id)->row();
   ?>
  <fieldset>
  <legend>Encaminhar protocolo</legend>
   <form action="" id="user2">
   <div class="row">
  <div class="small-6 columns">
        <div class="row">
              <div class="small-12 columns">
                    <label for="pt">Protocolo</label>
                    <input type="text" name="protocolo" id="protocolo" disabled="disabled" value="<?php echo $querys->id?>">  
              </div>
        </div>
        <div class="row">
              <div class="small-12 columns">
              <label for="area">Área</label>
                    <select name="area" id="area">
                    <?php 
                    $array = array();
                    $query = $this->area->get_all_areas()->result();
                    foreach ($query as $area){
                    ?>
                    <option value="<?php echo $area->id?>"><?php echo $area->area?></option>
                    <?php } ?>
                    </select>    
              </div>
        </div>
  </div>
  <div class="small-6 columns">
        <div class="row">
              <div class="small-12 columns">
                    <label for="numero">Número do documento</label>
                    <input type="text" name="documento" id="documento" disabled="disabled" value="<?php echo $querys->numerodocumento?>">
              </div>
        </div>
  <div class="row">
        <div class="small-12 columns">
              <label for="status">Status</label>
              <select name="status" id="status">
              <?php 
              $array = array();
              $query = $this->situacao->get_all_situacao()->result();
              foreach($query as $linha){
              ?>
              <option value="<?php echo $linha->id ?>"><?php echo $linha->situacao?></option>
              <?php } ?>
              </select>
        </div>
  </div>
        <input type="submit" value="Encaminhar" id="salvar" class="button radius small font encaminhar">
        <input type="hidden" name="idhistorico" value="<?php echo $querys->protocolo_id ;?>">
  </div>

Controller:

public function atualizarprotocolo(){
    $dados['area_id'] = $this->input->post('area');
    $dados['situacao_id'] = $this->input->post('status');
    $this->protocolo->atualizarhistorico($dados, array('protocolo_id' => $this->input->post('idhistorico')));        
}

Model:

public function atualizarhistorico($dados = NULL, $condicao = NULL, $redir = TRUE){
      if ($dados != NULL && is_array($condicao)):
         $this->db->update('historicotramitacao', $dados, $condicao);
        if ($this->db->affected_rows() > 0):
            set_msg('msgok', 'Alteração efeutada com sucesso!', 'sucesso');
        else:
            set_msg('msgerro', 'Erro ao alterar dados!', 'erro');
        endif;
        if ($redir)
            redirect(current_url()); 
    endif;
}

When I click the button, the alert message is displayed, however the update does not work. The message 'Error while changing data!' Appears. It looks like it is in the model function, but I can not see where the error is. Anyone have ideas?

    
asked by anonymous 27.01.2015 / 01:02

1 answer

1

Hello, I did not test, but I realized that you are doing the location.reload () before sending the ajax request, try to make the changes to see if it works.

$.ajax({
    type: "POST",
    url: destino,
    data: dados,
    success: function() {
        alert("Protocolo encaminhado com sucesso!");
        location.reload();
    }
});
    
27.01.2015 / 17:12