AJAX + PHP, Value received (date) put in html

Asked

Viewed 23 times

-1

It is giving undefined result, because I do not know how to put the value of $Validator[messages] (PHP) in the result of AJAX... I put it inside the html(.... " + data.messages + "...) but I don’t know if it’s like this...

  // AJAX - Deletar
  $(document).on("click", "#deleteEquip", function() {

    var dados = {
      buttonAcess: $('#acessDeleteEquip').val(),
      admin_id: $('#admin_id').val(),
    }

    $.ajax({
      url : "php_action/equip.php",
      type: "POST",
      data:  dados,
      async : true,
      cache : false,
      dataType : 'html',
      contentType: "application/x-www-form-urlencoded;charset=UTF-8",      
      success: function(data) {
        console.log(data);
        table.ajax.reload();      
        $('.modal-title').html('Deletar');
        if(data.success){
           $(".modal-body-info").html("<div class='alert alert-success ze-center'>Registro deletado com sucesso<br><i class='fas fa-thumbs-up ze-icon-g'></i></div>");     
        } else{
          $(".modal-body-info").html("<div class='alert alert-danger ze-center'>" + data.message + "<br><i class='fas fa-thumbs-down ze-icon-g'></i></div>");     
        }
        $('.modal-footer-button').html('');
        $("#myModalMessage").modal("show");
      },
      error: function(error) {
        console.error(error);
        $('.modal-title').html('Deletar');
        $(".modal-body-info").html("<div class='alert alert-danger ze-center'>Erro: Envio [3] (AJAX)<br><i class='fas fa-thumbs-down ze-icon-g'></i></div>");     
        $('.modal-footer-button').html('');
        $("#myModalMessage").modal("show");
      }    
    });
  });
  // AJAX - Deletar - End
if($buttonAcess == "acessDeleteEquip"){

$update = $datasource->deleteSelectedEquip($admin_id);  

if($update) {           
    $validator['sucess'] = true;
 } else {        
    $validator['sucess'] = false;
    $validator['messages'] = "Erro: Alguma informação incorreta";
}
} 

1 answer

0

Solved,

I’m putting it here for other people who need it.

  // AJAX - Deletar
  $(document).on("click", "#deleteEquip", function() {

var dados = {
  buttonAcess: $('#acessDeleteEquip').val(),
  admin_id: $('#admin_id').val(),
}

$.ajax({
  url : "php_action/equip.php",
  type: "POST",
  data:  dados,
  async : true,
  cache : false,
  dataType : 'html',
  contentType: "application/x-www-form-urlencoded;charset=UTF-8",      
  success: function(data) {
    console.log(data);
    table.ajax.reload();
    $('#modal-title').html('<i class="fa fa-trash-alt ze-icon-g" aria-hidden="true"></i> Deletar');
    var obj = $.parseJSON(data);   
    if(obj.sucess){
    $("#modal-body-info").html("<div class='alert alert-success ze-center'>" + obj.messages + "<br><i class='fas fa-thumbs-up ze-icon-g'></i></div>");     
    } else{
      $("#modal-body-info").html("<div class='alert alert-danger ze-center'>" + obj.messages + "<br><i class='fas fa-thumbs-down ze-icon-g'></i></div>");     
    }
    $('#modal-footer-button').html('');
    $("#myModalMessage").modal("show");
  },
  error: function(error) {
    console.error(error);
    $('#modal-title').html('<i class="fa fa-trash-alt ze-icon-g" aria-hidden="true"></i> Deletar');
    $("#modal-body-info").html("<div class='alert alert-danger ze-center'>Erro: Envio (AJAX)<br><i class='fas fa-thumbs-down ze-icon-g'></i></div>");     
    $('#modal-footer-button').html('');
    $("#myModalMessage").modal("show");
  }    
});
  });
  // AJAX - Deletar - End
if($buttonAcess == "acessDeleteEquip"){

$update = $datasource->deleteSelectedEquip($admin_id);  

if($update) {           
    $validator['sucess'] = true;
    $validator['messages'] = "Sucesso: Registro deletado";

 } else {        
    $validator['sucess'] = false;
    $validator['messages'] = "Erro: Alguma informação incorreta";
}
} 

Browser other questions tagged

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