setTemplateDir('../ihtml'); $smarty->setCompileDir('../compile'); $errorMsgs = []; $errorCheck = false; $number = $_POST['number'] ?? ''; $email = $_POST['email'] ?? ''; $msg = ''; function insertDataOnTable(mysqli $mysql, string $email, string $number) { $query = "INSERT INTO Contact VALUES (NULL, '$email', '$number');"; try { $mysql->query($query); } catch (Exception $e) { echo $e->getMessage(); } } 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; } if($_SERVER['REQUEST_METHOD'] === 'POST') { $errorCheck = controlloErrori($email, $number, $errorMsgs); if(count($errorMsgs) == 0) { try { $conn = new mysqli('127.0.0.1', 'root', '', 'contact_db'); } catch (Exception $e) { die($e->getMessage()); } 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('errorMsgs', $errorMsgs); $smarty->display('contact.tpl'); ?>