Salve, sto testando un form realizzato con swiftmailer a questa pagina:
http://www.demaniomarittimo.com/offe...rta-lavoro.php
Il form è a fondo pagina e con esso tutti i messaggi di success sent e di validation.
Ogni volta che si clicca su invia la pagina subisce una sorta di refresh e la pagina viene riportata al top facendo sparire sia il form che i vari messaggi di cui sopra.
Posto il file send.php di seguito:
Codice PHP:
<?php// se il form e' stato inviato...if(isset($_POST['invia'])){ /************************************/ /* VARIABILE PER GESTIRE GLI ERRORI */ /************************************/ // la impostiamo FALSE // se durante la procedura diventa vera la modifichiamo in TRUE $error = FALSE; /********************************/ /* RICEVIAMO GLI INPUT DAL FORM */ /********************************/ $nome = isset($_POST['nome']) ? trim( (string) $_POST['nome']) : ''; $cognome = isset($_POST['cognome']) ? trim( (string) $_POST['cognome']) : ''; $oggetto = isset($_POST['oggetto']) ? trim( (string) $_POST['oggetto']) : ''; $email = isset($_POST['email']) ? trim( (string) $_POST['email']) : ''; $messaggio = isset($_POST['messaggio']) ? trim( (string) $_POST['messaggio']) : '';
/************************************/ /* VERIFICHIAMO I CAMPI OBBLIGATORI */ /************************************/ if(empty($nome)) { $error = TRUE; $msg = 'Il campo nome è obbligatorio!'; } else if(empty($cognome)){ $error = TRUE; $msg = 'Il campo cognome è obbligatorio'; } else if(empty($oggetto)){ $error = TRUE; $msg = 'Il campo oggetto è obbligatorio'; } else if(!preg_match('/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+$/', $email)){ $error = TRUE; $msg = 'Il campo email è obbligatorio. Verifica di averlo digitato correttamente.'; } else if(empty($messaggio)){ $error = TRUE; $msg = 'Il campo messaggio è obbligatorio'; } // siccome i campi obbligatori sono ok possiamo procedere a comporre lemail else{ /************************************************************/ /* INCLUDIAMO SWIFTMAILER E IL NOSTRO FILE DI CONFIGURAZIONE*/ /************************************************************/ require_once('lib/swift_required.php'); require_once('config.php'); /*****************************************************************/ /* IMPOSTIAMO IL SERVER SMTP CHE SI OCCUPERA' DI INVIARE L'EMAIL */ /*****************************************************************/ // i dati SMTP presenti nel config if( (PORT_SMTP != FALSE) AND ( SECUTITY_SMTP != FALSE ) ){ $transport = Swift_SmtpTransport::newInstance(HOST_SMTP, PORT_SMTP, SECUTITY_SMTP); } else if( PORT_SMTP != FALSE ){ $transport = Swift_SmtpTransport::newInstance(HOST_SMTP, PORT_SMTP); } else{ $transport = Swift_SmtpTransport::newInstance(HOST_SMTP); } $transport->setUsername(EMAIL_SMTP); $transport->setPassword(PASSWORD_SMTP); $mailer = Swift_Mailer::newInstance($transport);
/***************************/ /* COMPONIAMO IL MESSAGGIO */ /***************************/ $message = Swift_Message::newInstance($oggetto); // oggetto $message->setFrom(array($email => $nome)); // email e nome di provenienza $message->setTo(array(EMAIL_DESTINATARIO)); // destinatario/i sotto forma di array $message->setBody( '<html>' . '<head></head>' . '<body>' . 'Nome:' . $nome . '<br />'. 'Cognome:' . $cognome . '<br />'. 'Messaggio:' . $messaggio . '<br />'. '</body>' . '</html>', 'text/html' // Mark the content-type as HTML ); // corpo del messaggio // http://forums.devnetwork.net/viewtopic.php?f=52&t=98225&p=531623 $message->setReturnPath(EMAIL_SMTP); /************/ /* ALLEGATO */ /************/ // se esiste l'allegato if( (isset($_FILES["allegato"])) AND ($_FILES["allegato"]['name'] != '') ){ // indichiamo le tipologie di file consentiti $tipologie_consentite = array('jpg', 'jpeg', 'png', 'gif', 'doc', 'docx', 'pdf', 'xls', 'zip', 'rar'); // tutto minuscolo $path_info = pathinfo($_FILES["allegato"]['name']); // se si e' verificato un errore nell'upload... if(!is_uploaded_file($_FILES["allegato"]['tmp_name'])){ $error = TRUE; $msg = 'Si è verificato un errore durante il caricamento del file!'; } else if(!in_array(strtolower($path_info['extension']), $tipologie_consentite)){ $error = TRUE; $msg = 'Il file non è fra i tipi consentiti!'; } // se supera la dimensione massima consentita else if($_FILES["allegato"]['size'] > MAX_DIM_FILE){ // supera la dimensione massima consentita... $error = TRUE; $msg = 'Il file allegato eccede la dimensione massima consentita!'; } // altrimenti andiamo ad allegare il file else{ $attachment = Swift_Attachment::fromPath($_FILES["allegato"]['tmp_name']); $attachment->setFilename($_FILES["allegato"]['name']); $message->attach($attachment); } } }
/**************************************************/ /*SE NON SI SONO VERIFICATI ERRORI INVIAMO L'EMAIL*/ /**************************************************/ if($error === FALSE){ $result = $mailer->send($message); if($result>0){ $msg = "Email inviata. Presto sarai ricontattato."; } else{ $msg = "Problemi nell'invio della email!"; } }
/*********************************/ /* REDIRECT ALLA PAGINA DEL FORM */ /*********************************/ $get_var = urlencode($msg); header('location: offerta-lavoro.php?msg=' . $get_var); exit; }?>
Qualche aiuto su come risolvere il problema facendo si che la schermata resti sul form ad ogni submit?
Ringrazio in anticipo, Alx.