Snippets

Andrea Caravano Possibile soluzione all'esercizio "Maratona" in PHP

Created by Andrea Caravano
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
<?php
/*
 * Esercizio "Maratona" in PHP
 *
 * Testo dell'esercizio disponibile sul sito web dell'autore (www.andreacaravano.net)
 *
 * Ultima modifica: 30/06/2020
 *
 * Descrizione: Possibile soluzione all'esercizio "Maratona" in PHP
 * N.B.: L'esercizio scaturisce dalla sola fantasia dell'autore e intende rappresentare una applicazione didattica.
 * I dettagli in esso contenuti potrebbero non essere corrispondenti alla realtà e intendono valutare le abilità nella gestione delle strutture dati proposte.
 */
$cognome = "";
$dataNascita = "";
$comune = "";
$nazione = "";
$tipologiaDocumento = "";
$identificativoDocumento = "";
$codiceFiscale = "";
$numeroTessera = "";
$categoria = "";
$strumentiElettronici = "";
$mail = "";
$pass = "";
function tornaCheckBox($vettCheckBox)
{
    $ritorno = "";
    foreach ($vettCheckBox as $elemento) {
        $ritorno .= $elemento;
        if (array_search($elemento, $vettCheckBox) != count($vettCheckBox) - 1) {
            $ritorno .= ", ";
        }
    }
    return $ritorno;
}

function stampaPassword($pass)
{
    $ritorno = "";
    for ($i = 0; $i < strlen($pass); $i++) {
        if ($i == 0 || $i == strlen($pass) - 1 || $i == strlen($pass) - 2 || $i == strlen($pass) - 3) {
            $ritorno .= $pass[$i];
        } else $ritorno .= "*";
    }
    return $ritorno;
}

function controlloServer()
{
    global $cognome, $nome, $dataNascita, $comune, $nazione, $tipologiaDocumento, $identificativoDocumento, $codiceFiscale, $numeroTessera, $categoria, $strumentiElettronici, $mail, $pass, $esito, $continuaStampa;
    $continuaStampa = false;
    try {
        $cognome = $_REQUEST["cognome"];
        $nome = $_REQUEST["nome"];
        $dataNascita = $_REQUEST["data-nascita"];
        $comune = $_REQUEST["comune"];
        $nazione = $_REQUEST["nazione"];
        $tipologiaDocumento = $_REQUEST["tipologia-documento"];
        $identificativoDocumento = $_REQUEST["identificativo-documento"];
        $codiceFiscale = $_REQUEST["codice-fiscale"];
        $numeroTessera = $_REQUEST["numero-tessera"];
        $categoria = $_REQUEST["categoria"];
        $strumentiElettronici = $_REQUEST["strumenti-elettronici"];
        $mail = $_REQUEST["mail"];
        $pass = $_REQUEST["pass"];
        $regexDataNascita = "/^[0-9]{4}[-]{1}[0-9]{2}[-]{1}[0-9]{2}$/";
        $regexIdentificativoDocumento = "/^[A-Za-z]{2}[0-9]{1,}$/";
        $regexCodiceFiscale = "/^[A-Z]{6}[0-9]{2}[A-Z]{1}[0-9]{2}[A-Z]{1}[0-9]{3}[A-Z]{1}$/";
        $regexNumeroTessera = "/^[0-9]{9}129$/";
        $regexMail = "/^[a-z0-9._-]{1,}@[a-z0-9.-]{3,}\\.[a-z]{2,}$/";
        $regexCategoriaAtleta = "/^[1-7]$/";
        $contaErrori = 0;
        if (strlen($cognome) < 1 || strlen($cognome) > 150) {
            $vettErrori[$contaErrori] = "cognome";
            $contaErrori++;
        }
        if (strlen($nome) < 1 || strlen($nome) > 100) {
            $vettErrori[$contaErrori] = "nome";
            $contaErrori++;
        }
        if (preg_match($regexDataNascita, $dataNascita) == false) {
            $vettErrori[$contaErrori] = "Data di nascita";
            $contaErrori++;
        }
        if (strlen($comune) < 2) {
            $vettErrori[$contaErrori] = "Comune di nascita";
            $contaErrori++;
        }
        if (strlen($nazione) < 3) {
            $vettErrori[$contaErrori] = "Nazione di nascita";
            $contaErrori++;
        }
        if (isset($tipologiaDocumento) == false || ($tipologiaDocumento == "Carta d'identità" || $tipologiaDocumento == "Patente di guida") == false) {
            $vettErrori[$contaErrori] = "Tipologia documento";
            $contaErrori++;
        }
        if (preg_match($regexIdentificativoDocumento, $identificativoDocumento) == false) {
            $vettErrori[$contaErrori] = "Identificativo del documento";
            $contaErrori++;
        }
        if (preg_match($regexCodiceFiscale, $codiceFiscale) == false || strlen($codiceFiscale) != 16) {
            $vettErrori[$contaErrori] = "Codice Fiscale";
            $contaErrori++;
        }
        if (preg_match($regexNumeroTessera, $numeroTessera) == false || strlen($numeroTessera) != 12) {
            $vettErrori[$contaErrori] = "Numero tessera";
            $contaErrori++;
        }
        if (isset($categoria) == false || preg_match($regexCategoriaAtleta, $categoria) == false) {
            $vettErrori[$contaErrori] = "Categoria atleta";
            $contaErrori++;
        }
        if (count($strumentiElettronici) == 0 || (array_search("Fascia di rilevazione delle cardio-frequenze", $strumentiElettronici) >= 0 || array_search("Contapassi", $strumentiElettronici) >= 0 || array_search("Sensori di rilevazione dei grassi e calorie consumate", $strumentiElettronici) >= 0) == false) {
            $vettErrori[$contaErrori] = "Strumenti elettronici";
            $contaErrori++;
        }
        if (preg_match($regexMail, $mail) == false || strlen($mail) < 8) {
            $vettErrori[$contaErrori] = "E-mail";
            $contaErrori++;
        }
        if (strlen($pass) < 10 || strlen($pass) > 80) {
            $vettErrori[$contaErrori] = "Password";
            $contaErrori++;
        }
        if ($contaErrori > 0) {
            $continuaStampa = false;
            $esito = "<span style='color: #FF7070; font-weight: bold;'>Rilevati errori nei campi: ";
            for ($i = 0; $i < $contaErrori; $i++) {
                $esito .= $vettErrori[$i];
                if ($i != $contaErrori - 1)
                    $esito .= ", ";
            }
            $esito .= "!</span>";
        } else {
            $continuaStampa = true;
            $esito = "<span style='color: #339966; font-weight: bold;'>Registrazione conclusa con successo. Riepilogo dati:</span>";
        }
    } catch (Exception $e) {
        $continuaStampa = false;
        $esito = "<span style='color: #FF7070; font-weight: bold;'>Errore di elaborazione dei dati. Si prega di riprovare, verificando di aver fornito tutti i dati richiesti.</span>";
    }
    return $esito;
}

if (isset($_POST["submit"]) /* Alternativa: !empty($_REQUEST) */) {
    if ((isset($_REQUEST["cognome"]) && isset($_REQUEST["nome"]) && isset($_REQUEST["data-nascita"]) &&
            isset($_REQUEST["comune"]) && isset($_REQUEST["nazione"]) && isset($_REQUEST["tipologia-documento"]) &&
            isset($_REQUEST["identificativo-documento"]) && isset($_REQUEST["codice-fiscale"]) && isset($_REQUEST["numero-tessera"]) &&
            isset($_REQUEST["categoria"]) && isset($_REQUEST["strumenti-elettronici"]) && isset($_REQUEST["mail"]) && isset($_REQUEST["pass"])) == false) {
        $continuaStampa = false;
        $esito = "<span style='color: #FF7070; font-weight: bold;'>ATTENZIONE! Non sono stati compilati alcuni campi o non &egrave; stato possibile ricavarne i valori. Riprovare.</span>";
    } else
        $continuaStampa = true;
    ?>
    <!DOCTYPE html>
    <html lang="it">
        <head>
            <meta charset="UTF-8">
            <title>
                Registrazione alla maratona
            </title>
            <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
                  integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh"
                  crossorigin="anonymous">
            <style>
                td {
                    width: 50%;
                }

                th {
                    text-align: right;
                }
            </style>
        </head>
        <body>
            <main role="main" class="container" style="padding-top: 30px;">
                <h1 style="padding-bottom: 30px; text-align: center;">
                    Ricevuta di registrazione
                </h1>
                <table class="table table-striped table-bordered table-hover">
                    <caption>
                        Ricevuta di registrazione
                    </caption>
                    <thead>
                        <tr>
                            <td colspan="2">
                                <?php
                                if ($continuaStampa == false)
                                    echo $esito;
                                else {
                                    echo controlloServer();
                                }
                                ?>
                            </td>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        if ($continuaStampa) {
                            ?>
                            <tr>
                                <th>
                                    <label>
                                        Cognome:
                                    </label>
                                </th>
                                <td>
                                    <?php
                                    echo $cognome;
                                    ?>
                                </td>
                            </tr>
                            <tr>
                                <th>
                                    <label>
                                        Nome:
                                    </label>
                                </th>
                                <td>
                                    <?php
                                    echo $nome;
                                    ?>
                                </td>
                            </tr>
                            <tr>
                                <th>
                                    <label>
                                        Data di nascita:
                                    </label>
                                </th>
                                <td>
                                    <?php
                                    $dataNascita = explode("-", $dataNascita);
                                    $dataNascita = $dataNascita[2] . "/" . $dataNascita[1] . "/" . $dataNascita[0];
                                    echo $dataNascita;
                                    ?>
                                </td>
                            </tr>
                            <tr>
                                <th>
                                    <label>
                                        Comune di nascita:
                                    </label>
                                </th>
                                <td>
                                    <?php
                                    echo $comune;
                                    ?>
                                </td>
                            </tr>
                            <tr>
                                <th>
                                    <label>
                                        Nazione di nascita:
                                    </label>
                                </th>
                                <td>
                                    <?php
                                    echo $nazione;
                                    ?>
                                </td>
                            </tr>
                            <tr>
                                <th>
                                    <label>
                                        Tipologia di documento:
                                    </label>
                                </th>
                                <td>
                                    <?php
                                    echo $tipologiaDocumento;
                                    ?>
                                </td>
                            </tr>
                            <tr>
                                <th>
                                    <label>
                                        Identificativo del documento:
                                    </label>
                                </th>
                                <td>
                                    <?php
                                    echo $identificativoDocumento;
                                    ?>
                                </td>
                            </tr>
                            <tr>
                                <th>
                                    <label>
                                        Codice Fiscale:
                                    </label>
                                </th>
                                <td>
                                    <?php
                                    echo $codiceFiscale;
                                    ?>
                                </td>
                            </tr>
                            <tr>
                                <th>
                                    <label>
                                        Numero di tessera:
                                    </label>
                                </th>
                                <td>
                                    <?php
                                    echo $numeroTessera;
                                    ?>
                                </td>
                            </tr>
                            <tr>
                                <th>
                                    <label>
                                        Categoria di appartenenza:
                                    </label>
                                </th>
                                <td>
                                    <?php
                                    echo $categoria . "<sup>a</sup> categoria";
                                    ?>
                                </td>
                            </tr>
                            <tr>
                                <th>
                                    <label>
                                        Giornata della gara:
                                    </label>
                                </th>
                                <td>
                                    <?php
                                    switch ($categoria) {
                                        case 1:
                                            $giornata = "19 maggio";
                                            break;
                                        case 2:
                                            $giornata = "18 maggio";
                                            break;
                                        case 3:
                                            $giornata = "15 maggio";
                                            break;
                                        case 4:
                                            $giornata = "14 maggio";
                                            break;
                                        case 5:
                                            $giornata = "13 maggio";
                                            break;
                                        case 6:
                                            $giornata = "12 maggio";
                                            break;
                                        case 7:
                                            $giornata = "11 maggio";
                                            break;
                                        default:
                                            $giornata = "Errore nella determinazione della giornata di gara.";
                                            break;
                                    }
                                    echo $giornata;
                                    ?>
                                </td>
                            </tr>
                            <tr>
                                <th>
                                    <label>
                                        Numero di maglia (provvisorio):
                                    </label>
                                </th>
                                <td>
                                    <?php
                                    srand();
                                    echo rand(100, 900);
                                    ?>
                                </td>
                            </tr>
                            <tr>
                                <th>
                                    <label>
                                        Strumenti elettronici addizionali
                                    </label>
                                </th>
                                <td>
                                    <?php
                                    echo tornaCheckBox($strumentiElettronici);
                                    ?>
                                </td>
                            </tr>
                            <tr>
                                <th>
                                    <label>
                                        Indirizzo e-mail:
                                    </label>
                                </th>
                                <td>
                                    <?php
                                    echo $mail;
                                    ?>
                                </td>
                            </tr>
                            <tr>
                                <th>
                                    <label>
                                        Password:
                                    </label>
                                </th>
                                <td>
                                    <?php
                                    echo stampaPassword($pass);
                                    ?>
                                </td>
                            </tr>
                            <tr>
                                <th>
                                    <label>
                                        Codice univoco di prenotazione:
                                    </label>
                                </th>
                                <td>
                                    <?php
                                    /*
                                     Passi dell'algoritmo:
                                     1.	Trasformazione del cognome nelle corrispondenti codifiche decimali ASCII Standard (solo se stampabili, dunque esclusi spazi, caratteri speciali e non stampabili).
                                     2.	Trasformazione del nome nelle corrispondenti codifiche decimali ASCII Standard (solo se stampabili, dunque esclusi spazi, caratteri speciali e non stampabili).
                                     3.	Trasformazione della data di nascita nel corrispettivo formato concatenato GGMMAAAA.
                                     4.	Estrapolazione delle prime 9 cifre del numero di tessera
                                     */
                                    $codiceUnivoco = "";
                                    for ($i = 0; $i < strlen($cognome); $i++) {
                                        if (ctype_print($cognome[$i])) {
                                            $codiceUnivoco .= ord($cognome[$i]);
                                        }
                                    }
                                    $codiceUnivoco .= "-";
                                    for ($i = 0; $i < strlen($nome); $i++) {
                                        if (ctype_print($nome[$i])) {
                                            $codiceUnivoco .= ord($nome[$i]);
                                        }
                                    }
                                    $codiceUnivoco .= "-";
                                    $codiceUnivoco .= str_replace("/", "", $dataNascita);;
                                    $codiceUnivoco .= "-";
                                    $codiceUnivoco .= substr($numeroTessera, 0, 9);
                                    echo $codiceUnivoco;
                                    ?>
                                </td>
                            </tr>
                            <tr>
                                <th>
                                    <label>
                                        Data e orario correnti:
                                    </label>
                                </th>
                                <td>
                                    <?php
                                    echo date("d/m/Y - H:i:s")
                                    ?>
                                </td>
                            </tr>
                            <?php
                        }
                        ?>
                    </tbody>
                </table>
            </main>
            <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"
                    integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n"
                    crossorigin="anonymous"></script>
            <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"
                    integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo"
                    crossorigin="anonymous"></script>
            <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"
                    integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6"
                    crossorigin="anonymous"></script>
        </body>
    </html>
    <?php
    if (strlen($esito) != 0)
        return;
} else {
    ?>
    <!DOCTYPE html>
    <html lang="it">
        <head>
            <meta charset="UTF-8">
            <title>
                Registrazione alla maratona
            </title>
            <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
                  integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh"
                  crossorigin="anonymous">
            <style>
                td {
                    width: 50%;
                }

                th {
                    text-align: right;
                }
            </style>
            <script type="text/javascript">
                function controlloClient() {
                    try {
                        var cognome = document.modulo.cognome.value;
                        var nome = document.modulo.nome.value;
                        var dataNascita = document.getElementById("data-nascita").value;
                        var comuneNascita = document.modulo.comune.value;
                        var nazioneNascita = document.modulo.nazione.value;
                        var tipologiaDocumento = tornaRadioCheckbox(document.getElementsByName("tipologia-documento"));
                        var identificativoDocumento = document.getElementById("identificativo-documento").value;
                        var codiceFiscale = document.getElementById("codice-fiscale").value;
                        var numeroTessera = document.getElementById("numero-tessera").value;
                        var categoriaAtleta = tornaSelect(document.modulo.categoria);
                        var strumentiElettronici = tornaRadioCheckbox(document.getElementsByName("strumenti-elettronici[]"));
                        var mail = document.modulo.mail.value;
                        var pass = document.modulo.pass.value;
                        var regexDataNascita = new RegExp("^[0-9]{4}[-]{1}[0-9]{2}[-]{1}[0-9]{2}$");
                        var regexIdentificativoDocumento = new RegExp("^[A-Za-z]{2}[0-9]{1,}$");
                        var regexCodiceFiscale = new RegExp("^[A-Z]{6}[0-9]{2}[A-Z]{1}[0-9]{2}[A-Z]{1}[0-9]{3}[A-Z]{1}$");
                        var regexNumeroTessera = new RegExp("^[0-9]{9}129$");
                        var regexMail = new RegExp("^[a-z0-9._-]{1,}@[a-z0-9.-]{3,}\\.[a-z]{2,}$");
                        var regexCategoriaAtleta = new RegExp("^[1-7]$");
                        var contaErrori = 0;
                        if (cognome.length < 1 || cognome.length > 150) {
                            alert("Errore sul campo cognome!");
                            document.modulo.cognome.focus();
                            contaErrori++;
                        }
                        if (nome.length < 1 || nome.length > 100) {
                            alert("Errore sul campo nome!");
                            document.modulo.nome.focus();
                            contaErrori++;
                        }
                        if (regexDataNascita.test(dataNascita) == false) {
                            alert("Errore sul campo Data di nascita!");
                            document.getElementById("data-nascita").focus();
                            contaErrori++;
                        }
                        if (comuneNascita.length < 2) {
                            alert("Errore sul campo Comune di nascita!");
                            document.modulo.comune.focus();
                            contaErrori++;
                        }
                        if (nazioneNascita.length < 3) {
                            alert("Errore sul campo Nazione di nascita!");
                            document.modulo.nazione.focus();
                            contaErrori++;
                        }
                        if (tipologiaDocumento.length == 0 || (tipologiaDocumento == "Carta d'identità" || tipologiaDocumento == "Patente di guida") == false) {
                            alert("Errore sul campo Tipologia documento!");
                            contaErrori++;
                        }
                        if (regexIdentificativoDocumento.test(identificativoDocumento) == false || identificativoDocumento.length > 15) {
                            alert("Errore sul campo Identificativo del documento!");
                            document.getElementById("identificativo-documento").focus();
                            contaErrori++;
                        }
                        if (regexCodiceFiscale.test(codiceFiscale) == false || codiceFiscale.length != 16) {
                            alert("Errore sul campo Codice Fiscale!");
                            document.getElementById("codice-fiscale").focus();
                            contaErrori++;
                        }
                        if (regexNumeroTessera.test(numeroTessera) == false || numeroTessera.length != 12) {
                            alert("Errore sul campo Numero tessera!");
                            document.getElementById("numero-tessera").focus();
                            contaErrori++;
                        }
                        if (categoriaAtleta.length == 0 || regexCategoriaAtleta.test(categoriaAtleta) == false) {
                            alert("Errore sul campo Categoria atleta!");
                            contaErrori++;
                        }
                        if (strumentiElettronici.length == 0 || (strumentiElettronici.includes("Fascia di rilevazione delle cardio-frequenze") || strumentiElettronici.includes("Contapassi") || strumentiElettronici.includes("Sensori di rilevazione dei grassi e calorie consumate")) == false) {
                            alert("Errore sul campo Strumenti elettronici!");
                            contaErrori++;
                        }
                        if (regexMail.test(mail) == false || mail.length < 8) {
                            alert("Errore sul campo E-mail!");
                            document.modulo.mail.focus();
                            contaErrori++;
                        }
                        if (pass.length < 10 || pass.length > 80) {
                            alert("Errore sul campo Password!");
                            document.modulo.pass.focus();
                            contaErrori++;
                        }
                        console.log("Identificati " + contaErrori + " errori.");
                        if (contaErrori > 0) {
                            return false;
                        }
                    } catch (ERR) {
                        console.warn("Eccezione non gestita: " + ERR);
                        return false;
                    }
                }

                function tornaRadioCheckbox(elemento) {
                    var ritorno = "";
                    var contaSelezionati = 0;
                    for (var j = 0; j < elemento.length; j++) {
                        if (elemento[j].checked) {
                            contaSelezionati++;
                        }
                    }
                    var contaRitornati = 0;
                    for (var i = 0; i < elemento.length; i++) {
                        if (elemento[i].checked) {
                            ritorno += elemento[i].value;
                            contaRitornati++;
                            if (contaSelezionati != contaRitornati)
                                ritorno += ", ";
                        }
                    }
                    return ritorno;
                }

                function tornaSelect(elemento) {
                    return elemento.options[elemento.selectedIndex].value;
                }
            </script>
        </head>
        <body>
            <main role="main" class="container" style="padding-top: 30px;">
                <h1 style="padding-bottom: 30px; text-align: center;">
                    Modulo di registrazione alla maratona
                </h1>
                <form id="modulo" name="modulo" action="<?php echo $_SERVER["PHP_SELF"] ?>" method="POST"
                      onsubmit="return controlloClient();">
                    <div class="form-group">
                        <table class="table table-striped table-bordered table-hover">
                            <caption>
                                Modulo di registrazione
                            </caption>
                            <tbody>
                                <tr>
                                    <th>
                                        <label for="cognome">
                                            Cognome:
                                        </label>
                                    </th>
                                    <td>
                                        <input type="text" id="cognome" name="cognome" minlength="1" maxlength="150"
                                               size="80" required/>
                                    </td>
                                </tr>
                                <tr>
                                    <th>
                                        <label for="nome">
                                            Nome:
                                        </label>
                                    </th>
                                    <td>
                                        <input type="text" id="nome" name="nome" minlength="1" maxlength="100"
                                               size="60"
                                               required/>
                                    </td>
                                </tr>
                                <tr>
                                    <th>
                                        <label for="data-nascita">
                                            Data di nascita:
                                        </label>
                                    </th>
                                    <td>
                                        <input type="date" id="data-nascita" name="data-nascita"
                                               pattern="^[0-9]{4}[-]{1}[0-9]{2}[-]{1}[0-9]{2}$" required/>
                                    </td>
                                </tr>
                                <tr>
                                    <th>
                                        <label for="comune">
                                            Comune di nascita:
                                        </label>
                                    </th>
                                    <td>
                                        <input type="text" id="comune" name="comune" minlength="2" required/>
                                    </td>
                                </tr>
                                <tr>
                                    <th>
                                        <label for="nazione">
                                            Nazione di nascita:
                                        </label>
                                    </th>
                                    <td>
                                        <input type="text" id="nazione" name="nazione" minlength="3" required/>
                                    </td>
                                </tr>
                                <tr>
                                    <th>
                                        <label>
                                            Tipologia di documento:
                                        </label>
                                    </th>
                                    <td>
                                        <input type="radio" id="carta-identita" name="tipologia-documento"
                                               value="Carta d&apos;identit&agrave;" checked/>
                                        <label for="carta-identita">
                                            Carta d&apos;identi&agrave;
                                        </label>
                                        <br/>
                                        <input type="radio" id="patente" name="tipologia-documento"
                                               value="Patente di guida"/>
                                        <label for="patente">
                                            Patente di guida
                                        </label>
                                    </td>
                                </tr>
                                <tr>
                                    <th>
                                        <label for="identificativo-documento">
                                            Identificativo del documento:
                                        </label>
                                    </th>
                                    <td>
                                        <input type="text" id="identificativo-documento"
                                               name="identificativo-documento"
                                               maxlength="15" pattern="^[A-Za-z]{2}[0-9]{1,}$" required/>
                                    </td>
                                </tr>
                                <tr>
                                    <th>
                                        <label for="codice-fiscale">
                                            Codice Fiscale:
                                        </label>
                                    </th>
                                    <td>
                                        <input type="text" id="codice-fiscale" name="codice-fiscale" minlength="16"
                                               maxlength="16" size="16"
                                               pattern="^[A-Z]{6}[0-9]{2}[A-Z]{1}[0-9]{2}[A-Z]{1}[0-9]{3}[A-Z]{1}$"
                                               required
                                        />
                                    </td>
                                </tr>
                                <tr>
                                    <th>
                                        <label for="numero-tessera">
                                            Numero di tessera:
                                        </label>
                                    </th>
                                    <td>
                                        <input type="number" id="numero-tessera" name="numero-tessera"
                                               minlength="12"
                                               maxlength="12" size="12" pattern="^[0-9]{9}129$" required/>
                                    </td>
                                </tr>
                                <tr>
                                    <th>
                                        <label for="categoria">
                                            Categoria di appartenenza:
                                        </label>
                                    </th>
                                    <td>
                                        <select id="categoria" name="categoria">
                                            <?php
                                            for ($i = 1; $i <= 7; $i++) {
                                                echo "<option value='" . $i . "'" . (($i == 1) ? " selected='selected'" : "") . ">" . $i . "&deg; categoria</option>";
                                            }
                                            ?>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <th>
                                        <label>
                                            Strumenti elettronici addizionali
                                        </label>
                                    </th>
                                    <td>
                                        <input type="checkbox" id="fascia-cardio" name="strumenti-elettronici[]"
                                               value="Fascia di rilevazione delle cardio-frequenze"/>
                                        <label for="fascia-cardio">
                                            Fascia di rilevazione delle cardio-frequenze
                                        </label>
                                        <br/>
                                        <input type="checkbox" id="contapassi" name="strumenti-elettronici[]"
                                               value="Contapassi"/>
                                        <label for="contapassi">
                                            Contapassi
                                        </label>
                                        <br/>
                                        <input type="checkbox" id="sensori-grassi-calorie"
                                               name="strumenti-elettronici[]"
                                               value="Sensori di rilevazione dei grassi e calorie consumate"/>
                                        <label for="sensori-grassi-calorie">
                                            Sensori di rilevazione dei grassi e calorie consumate
                                        </label>
                                    </td>
                                </tr>
                                <tr>
                                    <th>
                                        <label for="mail">
                                            Indirizzo e-mail:
                                        </label>
                                    </th>
                                    <td>
                                        <input type="email" id="mail" name="mail" minlength="8"
                                               pattern="^[a-z0-9._-]{1,}@[a-z0-9.-]{3,}\.[a-z]{2,}$" required/>
                                    </td>
                                </tr>
                                <tr>
                                    <th>
                                        <label for="pass">
                                            Password:
                                        </label>
                                    </th>
                                    <td>
                                        <input type="password" id="pass" name="pass" minlength="10" maxlength="80"
                                               required/>
                                    </td>
                                </tr>
                                <tr>
                                    <td style="text-align: right;">
                                        <input type="submit" name="submit" class="btn btn-primary"
                                               value="Conferma invio dati"/>
                                    </td>
                                    <td>
                                        <input type="reset" class="btn btn-secondary"
                                               value="Azzera i campi del modulo"/>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </form>
            </main>
            <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"
                    integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n"
                    crossorigin="anonymous"></script>
            <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"
                    integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo"
                    crossorigin="anonymous"></script>
            <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"
                    integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6"
                    crossorigin="anonymous"></script>
        </body>
    </html>
    <?php
}
?>

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.