G. formulario.html

1
<!DOCTYPE html>
2
<html lang="es">
3
4
<head>
5
6
 <meta charset="UTF-8">
7
 <meta name="viewport" content="width=device-width">
8
9
 <title>Servicio que procesa un formulario con token</title>
10
11
 <script src="dompurify/purify.min.js"></script>
12
 <script type="module" src="libclienteweb/manejaErrores.js"></script>
13
14
</head>
15
16
<body>
17
18
 <form id="formulario">
19
20
  <h1>Servicio que procesa un formulario con token</h1>
21
22
  <p><a href="index.html">Cancela</a></p>
23
24
  <!-- Al enviar la forma, se envía el token recibido. -->
25
  <input type="hidden" name="x">
26
27
  <p>
28
   <label>
29
    Saludo:
30
    <input name="saludo">
31
   </label>
32
  </p>
33
34
  <p>
35
   <label>
36
    Nombre:
37
    <input name="nombre">
38
   </label>
39
  </p>
40
41
  <p>
42
   <button type="reset">Restaurar</button>
43
   <button type="submit">Procesa</button>
44
  </p>
45
46
 </form>
47
48
 <script type="module">
49
50
  import { consume } from "./libclienteweb/consume.js"
51
  import { descargaVista } from "./libclienteweb/descargaVista.js"
52
  import { submitFormRecibeJson } from "./libclienteweb/submitFormRecibeJson.js"
53
54
  descargaDatos()
55
56
  async function descargaDatos() {
57
58
   // Al cargar, registra este formulario en la sesión y recibe un token.
59
   await descargaVista("api/vista-formulario.php")
60
61
   formulario.addEventListener("submit", procesa)
62
63
   async function procesa(event) {
64
    event.preventDefault()
65
    const resultado =
66
     await consume(submitFormRecibeJson("api/procesa.php", formulario))
67
    const mensaje = await resultado.json()
68
    alert(mensaje)
69
    location.href = "index.html"
70
   }
71
72
  }
73
74
 </script>
75
76
</body>
77
78
</html>
skip_previous skip_next