Close a modal when comparing what profile you have

1

I have a modal form, in which when you press save (submit) the fields are checked, type that you do not have any empty and such and then the values are sent.

I would like to make, within my function, that when everything is correct, we look at a variable of $ _session that contains a profile, if it is a profile it is "invited" you get an alert ("You do not have permissions! ") and the modal is closed (without sending anything obviously)

How could I fix my function to do that?

This is my modal with onsubmit="return validaCampos();">

<div class="modal" id="nuevoUsu" tabindex="-1" role="dialog" aria-labellebdy="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title"><span class="glyphicon glyphicon-plus-sign"></span>Nuevo Producto</h4>                       
                </div>
                <div class="modal-body">
                   <form action="insertar.php" method="GET" onsubmit="return validaCampos();"> 


                           <!--campos del formulario--->


                        <input type="submit" class="btn btn-success" value="Salvar">
                   </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-warning" data-dismiss="modal">Cerrar</button>
                </div>
            </div>
        </div>
    </div> 

Here I have the feature

        <script>

        function validaCampos(){
            //etc
            //etc
            //etc
            if(cantidad < 0){
            toastr.error("Mínimo permitido 0","Aviso!");
                return false;
            }
            ////////////////////// Aquí supongo que seria la funcion
             <?php 
             if($_SESSION['perfil']=="invitado"){
             ?>
             alert("No tienes permisos, eres invitado");
             dialog.dismiss();
             <?php 
             }
             ?>
            /////////////////////
            }
            </script>

The alert is shown to me but the modal is not closed and the data is sent

How could I solve it?

Thank you.

    
asked by Vidal 22.03.2018 в 12:15
source

2 answers

0

Since you're using Bootstrap it's best if you use modal

The idea is something like this:

//Si queremos que se cierre la de nuevo usuario después de la de sin permisos
            $('#divSinPermisos').on('hide.bs.modal', function () { 
          $('#nuevoUsu').modal('hide');
      });  

//Aquí iría la lógica de la $_SESSION
            if(true)
            {
              $('#divSinPermisos').modal('show');
              //Lo cerramos a los 3 segundos
              console.log($('#divSinPermisos'));
                   setTimeout(function() {
                    $('#divSinPermisos').modal('hide');                   
                }, 3000);
             //Cerramos el modal principal

             return false;
             }
           }

Here is a fiddle

I think you were missing the return false share to avoid the submit

    
answered by 22.03.2018 / 14:01
source
1

The function you use to close the modal does not exist, that's why it only shows you the alert but the modal one is not closed. What you should use is dialog.modal("hide"); to close it. As an example I include the variables of quantity and dialog that I assume must be the modal and it works perfectly. Source: Boostrap Modal

function validaCampos() {
  //etc
  var cantidad = 0;
  var dialog = $("#nuevoUsu");
  if (cantidad < 0) {
    alert("Mínimo permitido 0", "Aviso!");
    return false;
  }
  ////////////////////// Aquí supongo que seria la funcion

  if ("invitado" == "invitado") { // a modo de ejemplo pongo esto

    alert("No tienes permisos, eres invitado");
    dialog.modal("hide");
    return false;
  }

  /////////////////////
}
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
 <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#nuevoUsu">Abrir Modal</button>
 
<div class="modal" id="nuevoUsu" tabindex="-1" role="dialog" aria-labellebdy="myModalLabel" aria-hidden="false">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title"><span class="glyphicon glyphicon-plus-sign"></span>Nuevo Producto</h4>
      </div>
      <div class="modal-body">
        <form action="insertar.php" method="GET" onsubmit="return validaCampos()">


          <!--campos del formulario--->


          <input type="submit" class="btn btn-success" value="Salvar">
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-warning" data-dismiss="modal">Cerrar</button>
      </div>
    </div>
  </div>
</div>
    
answered by 22.03.2018 в 16:27