How to update via ajax + php

Asked

Viewed 1,515 times

2

I want to update data through a modal, however it is not updating. Does anyone have any idea why it is 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 message from Alert is shown, however the update does not work. The message 'Error when changing data!' is displayed. It seems to me that it’s in the model function, but I can’t see where the bug is. Does anyone have any idea?

1 answer

1

Hello, I haven’t tried it, but I noticed that you are doing 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();
    }
});
  • I tested it, it was the same way. displaying the model’s message. apparently the model is correct, I’ve done and I’ve redone this code several times and I don’t find the error!

  • Manage to solve the problem. I had forgotten to pass the value of the field Hidden(hidden) in ajax, IE, id was not being passed...hehehe!

  • But that it was strange to give a successful message before receiving the response from the server, was.

  • I was, I didn’t realize. But what matters is that it was solved!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.