progetto di prova che mostra le macchine fiat e un form che permette di contattare
filippo.bertilotti
2024-04-30 40302288abc170ebfd2b7ffacb334768f77b8698
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
<?php
    require '../vendor/autoload.php';
    use Smarty\Smarty;
    $smarty = new Smarty();
 
    $smarty->setTemplateDir('../ihtml');
    $smarty->setCompileDir('../compile');
 
    $errorMsgs = [];
    $validazioneOk = true;
 
    $number = $_POST['number'] ?? '';
    $email = $_POST['email'] ?? '';
    $msg = '';
 
 
    function insertDataOnTable(mysqli $mysql, string $email, string $number) { 
        $esitoInserimento = false;
        $query = "INSERT INTO Contact VALUES (NULL, '$email', '$number');";
        try {
            $mysql->query($query);
            $esitoInserimento = true;
        } catch (Exception $e) {
            echo $e->getMessage();
        }
        return $esitoInserimento;
    }
 
    function controlloErrori(string $email, string $number, array &$errorMsgs) {
        if(!is_numeric($number) || strlen($number) != 10) {
            $errorMsgs['number'] = 'Numero di telefono non corretto';
        }else {
            //$errorMsgs['number'] = '';
        }
    
        if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $errorMsgs['email'] = 'Email non valida';
        } else {
            //$errorMsgs['email'] = '';
        }
 
        $validazioneOk = count($errorMsgs) == 0;
 
        return $validazioneOk;
    }
    
 
 
    if($_SERVER['REQUEST_METHOD'] === 'POST') {
        $validazioneOk = controlloErrori($email, $number, $errorMsgs);
 
        if($validazioneOk) {
            try {
                $conn = new mysqli('127.0.0.1', 'root', '', 'contact_db');
            } catch (Exception $e) {
                die($e->getMessage());
            }
 
            $esitoInserimento = insertDataOnTable($conn, $email, $number);
 
            if($esitoInserimento == true) {
                $smarty->display('conferma.tpl');
                exit;
            }
        }
    }
    
    
 
    $smarty->assign('cellNumber', $number);
    $smarty->assign('email', $email);
    $smarty->assign('formMsg', $msg);
    
    $smarty->assign('errorMsgs', $errorMsgs);
 
    $smarty->display('contact.tpl');
  
    
?>