3
I made a code Javascript to enable some buttons and inputs in a FORM, according to the user’s permission, stored in a SESSION. Had done so:
function showEditButtons() 
{
   if ( verUserPermissions(8) === true )
   {
      btnConfirma.style.display = 'inline';
      btnCancela.style.display = 'inline';
      btnEdita.style.display = 'none';
      $('input.form-control:text').attr("disabled", false);
   }
}
function verUserPermissions(nivel){
   $.ajax({
       url: './funcoes/processaMaxDescAcresc.php',
       type: 'POST',
       data: {'OP':'NIVEL', 'NIVEL_ESPERADO':nivel},
       cache: false,
       dataType: 'json',
       success: function(data, textStatus, jqXHR)
       {
          var result = (data.nivel_permitido == '1');
          if ( ! result )
          {
             abreModaldeAviso('Falha', 'Usuário não autorizado a alterar estes valores.', 'alerta');
          }
          return result;
       },
       error: function(jqXHR, textStatus, errorThrown)
       {
          var result = false;
          abreModaldeAviso('Falha', 'Erro ao obter dados do usuario.', 'alerta');
          return result;
       }
   });
};
The function PHP ./functions/processMaxDescAcresc.php correctly returns the user level check. But despite the function verUserPermissions, return true, did not enable buttons and inputs in the function showEditButtons.
If I move the function code showEditButtons into the other function, in this case eliminating the function showEditButton, It works, it works like this:
function showEditButtons() {
   verUserPermissions(8);
}
function verUserPermissions(nivel){
   $.ajax({
      ...
      success: function(data, textStatus, jqXHR)
      {
         var result = (data.nivel_permitido == '1');
         if ( ! result ){
            abreModaldeAviso('Falha', 'Usuário não autorizado a alterar estes valores.', 'alerta');
         } else {
            btnConfirma.style.display = 'inline';
            btnCancela.style.display = 'inline';
            btnEdita.style.display = 'none';
            $('input.form-control:text').attr("disabled", false);
         }
      },
      error: function(jqXHR, textStatus, errorThrown)
      {
         var result = false;
         abreModaldeAviso('Falha', 'Erro ao obter dados do usuario.', 'alerta');
         return result;
      }
   });
};
My question is, why doesn’t it work? I don’t know much about Javascript and ajax, but in my opinion, it should work.
Good... very good to know, sometimes some details of some languages end up getting in the way.
– Celso Marigo Jr
uma vez que ajax é, por defeito, asincrono?? defect?– Daniel Omine
@Danielomine was literally translated, I wanted to say
padrão. Good point :)– MoshMage
translate "default" as "default" is something very distinct..
– Daniel Omine
@Danielomine happens when I don’t translate what I think, like "donate" instead of "donate" (Donate); it’s one of those stupid flaws :)
– MoshMage
I understand, but it has nothing to do with these justifications.
– Daniel Omine