I want to add an alert message from 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 https://sweetalert.js.org/guides/ I have managed to insert a successful alert message "success".
swal("¡OK!", "¡Teléfono modificado correctamente!", "success");
I have the error because I declare the swal()
before calling the corresponding library in the HTML, but nevertheless, if I place the message after the head
, then it does not work as I wish.
Clicking "Modify" should trigger 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 button code if(isset($_POST["modificar"])){
after <input type="submit" value="Modificar" name="modificar" class="registrar"/>
, it shows me the alert()
but it doesn't manage the operation performed until I press the "Modify" button again. To make the first modification, you need two clicks on "Modify". What would be the possible solution?
You are mixing PHP code with Javascript code, hence PHP does not have such a function defined.
One way to fix your problem is to postpone the call at the end of the document like so:
In this way, the necessary Javascript code will be generated to show the alert only if it is required. I have used
json_encode
to convert PHP data to Javascript data safely.