Fatal error: Call to undefined function swal () - PHP

4

I want to add an alert message of the SweetAlert class but when everything is correct and I want to show the message / popup it tells me the following error:

  

Fatal error: Call to undefined function swal ()

Using the page link I have managed to insert a satisfactory alert message "success".

swal("¡OK!", "¡Teléfono modificado correctamente!", "success");

I have the error because I declare before swal() before calling in the HTML to the corresponding library, but nevertheless, if I put the message after the head , then it does not work as I wish.

When you click on "Modify" you should skip the alert swal() .

Code:

<?php
    header('Content-Type: text/html; charset=UTF-8');

    //Por defecto no mostraremos el mensaje.
    $mostrar = false;
    //Si pulsamos el botón "Modificar"...
    if(isset($_POST["modificar"])){
        $telefono = $_POST["telefono"];
        //Llamamos al método "modificarCliente" y le pasamos el parámetro (teléfono).
        BD::modificarCliente($cliente, $telefono);
        //Si debemos mostrar el mensaje, la estructura es la siguiente.
        $mostrar = ["¡OK!", "¡Teléfono modificado correctamente!", "success"];
    }
?>

<!DOCTYPE html>
<html lang="es">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Panel del cliente</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>     
    </head>

    <body>
        <div id="menu_cliente">
            <ul id="menu_horizontal">
                <li class="nueva" id="nueva"><a class="active" href="menu_cliente.php?nueva=1#nueva">NUEVA OPINIÓN</a></li>
                <li class="ver" id="ver"><a href="menu_cliente.php?ver=1#ver">VER OPINIONES</a></li>
                <li class="eliminar" id="eliminar"><a href="menu_cliente.php?eliminar=1#eliminar">ELIMINAR OPINIÓN</a></li>
                <li class="datos_cliente" id="datos_cliente"><a href="menu_cliente.php?datos_cliente=1#datos_cliente">DATOS PERSONALES</a></li>
            </ul>

            <div id="cuerpo_body">
                <?php
                //Si pulsamos el link "Datos personales"...
                if(isset($_GET["datos_cliente"])){
                    //Obtengo todos los datos del cliente.
                    $objeto_cliente = BD::obtenerCliente($cliente); ?>
                <form action="menu_cliente.php?datos_cliente=1" name="miformulario" id="miformulario"  method="POST" class="form-register" onsubmit="return validar_telefono();">
                    <h2 class="form-titulo">MODIFICAR DATOS</h2>
                    <div class="contenedor-inputs">
                        <input type="text" name="telefono" id="telefono" maxlength="9" class="input-2" value="<?php echo $objeto_cliente->getTelefono();?>" onkeypress="return soloNumeros(event);">
                        <br/>
                        <input type="submit" value="Modificar" name="modificar" class="registrar"/>
                    </div>
                </form>
                <?php 
                }
                ?>
            </div>
            <?php
            //Si hemos definido un contenido para ser mostrado, lo utilizamos...
            if ($mostrar !== false) {
                ?>
                <script language="text/javascript">
                    swal(<?= json_encode($mostrar[0]) ?>, <?= json_encode($mostrar[1]) ?>, <?= json_encode($mostrar[2]) ?>);
                </script>
            <?php
            }
            ?>
        </div>
    </body>
</html>

If I put the code of the button if(isset($_POST["modificar"])){ after <input type="submit" value="Modificar" name="modificar" class="registrar"/> , it shows the alert() but does not manage the operation performed until you press the "Modify" button again. To make the first modification, you need two clicks in "Modify". What would be the possible solution?

    
asked by omaza1990 03.01.2018 в 14:06
source

1 answer

3

You are mixing PHP code with Javascript code, hence PHP has not defined that function.

One way to solve your problem is to postpone the call at the end of the document in the following way:

<?php
    header('Content-Type: text/html; charset=UTF-8');
    /* Por defecto no mostraremos el mensaje */
    $mostrar = false;
    //Si pulsamos el botón "Modificar"...
    if(!empty($_POST['telefono']) && !empty($_GET['datos_cliente'])){
        //Llamamos al método "modificarCliente" y le pasamos el parámetro (teléfono).
        BD::modificarCliente($_GET['datos_cliente'], $_POST['telefono']);
        /* Si debemos mostrar el mensaje lo marcamos así */
        $mostrar = ["¡OK!", "¡Teléfono modificado correctamente!", "success"];
    }
?>

<!DOCTYPE html>
<html lang="es">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Panel del cliente</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>     
    </head>
    <body>
        <div id="menu_cliente">
            <ul id="menu_horizontal">
                <li class="nueva" id="nueva"><a class="active" href="menu_cliente.php?nueva=1#nueva">NUEVA OPINIÓN</a></li>
                <li class="ver" id="ver"><a href="menu_cliente.php?ver=1#ver">VER OPINIONES</a></li>
                <li class="eliminar" id="eliminar"><a href="menu_cliente.php?eliminar=1#eliminar">ELIMINAR OPINIÓN</a></li>
                <li class="datos_cliente" id="datos_cliente"><a href="menu_cliente.php?datos_cliente=1#datos_cliente">DATOS PERSONALES</a></li>
            </ul>

            <div id="cuerpo_body">
                <?php
                //Si pulsamos el link "Datos personales"...
                if(isset($_GET["datos_cliente"])){
                    //Obtengo todos los datos del cliente.
                    $objeto_cliente = BD::obtenerCliente($cliente); ?>
                <form action="menu_cliente.php?datos_cliente=<?= url_encode($_GET["datos_cliente"]) ?>" name="miformulario" id="miformulario"  method="POST" class="form-register" onsubmit="return validar_telefono();">
                    <h2 class="form-titulo">MODIFICAR DATOS</h2>
                    <div class="contenedor-inputs">
                        <input type="text" name="telefono" id="telefono" maxlength="9" class="input-2" value="<?php echo $objeto_cliente->getTelefono();?>" onkeypress="return soloNumeros(event);">
                        <br/>
                        <input type="submit" value="Modificar" name="modificar" class="registrar"/>
                    </div>
                </form>
                <?php 
                }
                ?>
            </div>
        </div>
    </body>
<?php
/* Si hemos definido un contenido para $mostrar, lo usamos */
if ($mostrar !== false) {
?><script>
  swal(
    <?= json_encode($mostrar[0]) ?>,
    <?= json_encode($mostrar[1]) ?>,
    <?= json_encode($mostrar[2]) ?>
  );
</script>
<?php
}
</html>

This will generate the necessary Javascript code to show the alert only if it is required. I used json_encode to convert PHP data to Javascript data in a secure way.

    
answered by 03.01.2018 / 14:31
source