progetto di prova che mostra le macchine fiat e un form che permette di contattare
filippo.bertilotti
2024-04-30 f926d7469074808972d1f61464ee119660baf5dc
controllo errori e invio messaggio form
2 files modified
40 ■■■■ changed files
htdocs/contact.php 39 ●●●●● patch | view | raw | blame | history
ihtml/contact.tpl 1 ●●●● patch | view | raw | blame | history
htdocs/contact.php
@@ -6,14 +6,16 @@
    $smarty->setTemplateDir('../ihtml');
    $smarty->setCompileDir('../compile');
    $error = [];
    $errorMsgs = [];
    $errorCheck = false;
    $number = $_POST['number'] ?? '';
    $email = $_POST['email'] ?? '';
    $msg = '';
    function insertDataOnTable(mysqli $mysql, string $email, string $number) {
        $query = "INSERT INTO Contact VALUES ('$email', '$number');";
    function insertDataOnTable(mysqli $mysql, string $email, string $number) {
        $query = "INSERT INTO Contact VALUES (NULL, '$email', '$number');";
        try {
            $mysql->query($query);
        } catch (Exception $e) {
@@ -21,26 +23,33 @@
        }
    }
    function controlloErrori(string $email, string $number, array &$error) {
    function controlloErrori(string $email, string $number, array &$errorMsgs) {
        if(!is_numeric($number) || strlen($number) != 10) {
            $error['number'] = 'Numero di telefono non corretto';
            $errorMsgs['number'] = 'Numero di telefono non corretto';
            $errorCheck = true;
        }else {
            //$error['number'] = '';
            //$errorMsgs['number'] = '';
            $errorCheck = false;
        }
    
        if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $error['email'] = 'Email non valida';
            $errorMsgs['email'] = 'Email non valida';
            $errorCheck = true;
        } else {
            //$error['email'] = '';
            //$errorMsgs['email'] = '';
            $errorCheck = false;
        }
        return $errorCheck;
    }
    
    if($_SERVER['REQUEST_METHOD'] === 'POST') {
        controlloErrori($email, $number, $error);
        $errorCheck = controlloErrori($email, $number, $errorMsgs);
        if(count($error) == 0) {
        if(count($errorMsgs) == 0) {
            try {
                $conn = new mysqli('127.0.0.1', 'root', '', 'contact_db');
            } catch (Exception $e) {
@@ -49,14 +58,20 @@
            insertDataOnTable($conn, $email, $number);
        }
        if ($errorCheck == false) {
            $msg = "La contattatteremo presto!";
        }
    }
    
    
    $smarty->assign('cellNumber', $number);
    $smarty->assign('email', $email);
    $smarty->assign('formMsg', $msg);
    $smarty->assign('error', $error);
    $smarty->assign('errorMsgs', $errorMsgs);
    $smarty->display('contact.tpl');
?>
ihtml/contact.tpl
@@ -17,6 +17,7 @@
            <input type="text" name="number" placeholder="inserisci il numero"> <br>
            <button type="submit">clicca qui</button>
        </form>
        {$formMsg}
        {foreach from=$error item=errore key=campo}
            {$errore}
            <br>