how to use the Toasts of materialize

0

Greetings friends I would like to change the model of how to show my mjs of alerts for when I insert, delete and update. I would like to change these alerts for the Toasts of materialize. I append the code as I do to show the alerts. link of matrerialize link

Insert method

<!-- proceso para registrar-->
<?php
if(isset($_POST['guardar'])){


$sql = "SELECT cuentas FROM cuentas WHERE cuentas = :cuentas LIMIT 1"; //Creamos la select
$check = $DB_con->prepare($sql); //Preparamos la SELECT, de ésta manera evitamos SQL Injection
$check->bindParam(':cuentas', $_POST['cuentas']);//Substituimos las variables de la SELECT
$check->execute();//Ejecutamos la consulta
$contador = $check -> rowCount();//Esta función devuelve el número de resultados que ha devuelto la SELECT
if ($contador > 0) {
$check->closeCursor();

          $errMSG = "¡ Ups Aviso: El Registro ya se Encuentra Insertado !";

}
else
{

$sql=$DB_con->prepare("INSERT INTO cuentas (cuentas) VALUES (:cuentas)");
$sql->bindParam(':cuentas', $_POST['cuentas']);
$sql->execute();

      $successMSG ="¡ Bien Hecho: Registro Insertado Correctamente !";

    }
  }
?>
   

show mjs

<!-- mostrar mjs -->
<?php
if(isset($errMSG))
{
  ?>
  <div id="element" class='col s5 card-panel blue lighten-2 right'>
  <h6 class='black-text text-darken-2 center CONDENSED LIGHT5'>
  <?php echo $errMSG; ?> <a href='#' class="black-text" id="hide">[X]</a>
  </h6>
  </div>
  <?php
 }
 else if(isset($successMSG))
 {
   ?>
   <div id="element" class='col s5 card-panel teal lighten-2 right'>
   <h6 class='black-text text-darken-2 center CONDENSED LIGHT5'>
   <?php echo $successMSG; ?> <a href='#' class="black-text" id="hide">[X]
  </a>
   </h6>
   </div>
  <?php
  }
 ?>           
  <!-- FIN VALIDACION -->
    
asked by yoclens 06.08.2017 в 17:51
source

1 answer

0

In your show message you can apply it

<?php
if(isset($errMSG))
{
  ?>
  <a id="alerta" class="btn" onclick="Materialize.toast(<?php echo $errMSG; ?>, 4000)"></a>
<script>
   document.getElementById('alerta').click();
</script>
  <?php
 }
 else if(isset($successMSG))
 {
   ?>
   <a id="alerta" class="btn" onclick="Materialize.toast(<?php echo $successMSG; ?>, 4000)"></a>
<script>
   document.getElementById('alerta').click();
</script>
  <?php
  }
 ?>           
  <!-- FIN VALIDACION -->

You will load in message in the first parameter of Materialize.toast and launch the click event of the item a.alerta.

(if you can do it from php much better so that you avoid combining the languages).

    
answered by 15.08.2017 в 20:22