progetto di prova che mostra le macchine fiat e un form che permette di contattare
filippo.bertilotti
2024-04-30 40302288abc170ebfd2b7ffacb334768f77b8698
refactor
1 files modified
32 ■■■■ changed files
htdocs/contact.php 32 ●●●● patch | view | raw | blame | history
htdocs/contact.php
@@ -7,7 +7,7 @@
    $smarty->setCompileDir('../compile');
    $errorMsgs = [];
    $errorCheck = true;
    $validazioneOk = true;
    $number = $_POST['number'] ?? '';
    $email = $_POST['email'] ?? '';
@@ -15,48 +15,53 @@
    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';
            $errorCheck = true;
        }else {
            //$errorMsgs['number'] = '';
            $errorCheck = false;
        }
    
        if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $errorMsgs['email'] = 'Email non valida';
            $errorCheck = true;
        } else {
            //$errorMsgs['email'] = '';
            $errorCheck = false;
        }
        return $errorCheck;
        $validazioneOk = count($errorMsgs) == 0;
        return $validazioneOk;
    }
    
    if($_SERVER['REQUEST_METHOD'] === 'POST') {
        $errorCheck = controlloErrori($email, $number, $errorMsgs);
        $validazioneOk = controlloErrori($email, $number, $errorMsgs);
        if(count($errorMsgs) == 0) {
        if($validazioneOk) {
            try {
                $conn = new mysqli('127.0.0.1', 'root', '', 'contact_db');
            } catch (Exception $e) {
                die($e->getMessage());
            }
            insertDataOnTable($conn, $email, $number);
            $esitoInserimento = insertDataOnTable($conn, $email, $number);
            if($esitoInserimento == true) {
                $smarty->display('conferma.tpl');
                exit;
            }
        }
    }
    
@@ -68,10 +73,7 @@
    
    $smarty->assign('errorMsgs', $errorMsgs);
    if($errorCheck == false) {
        $smarty->display('conferma.tpl');
    }else {
        $smarty->display('contact.tpl');
    }
    $smarty->display('contact.tpl');
    
?>