Buonpomeriggio a tutti,
Sto uscendo pazza a risolvere questi due enigmi. Devo realizzare cun form contact. Ho recuperato dalla rete questo code che funziona alla grande, ma vorrei ottenere un risultato differente.
Per prima cosa vi copio il mio progetto:
codice HTML:
<form action="contact.php" method="post" >
<div class="contact-grid">
<div class="col-md-6 contact-us">
<input type="text" value="Nome" name="nome" onFocus="this.value='';" onBlur="if (this.value == '') {this.value = 'Name';}"required >
</div>
<div class="col-md-6 contact-us">
<input type="text" value="Email" name="email" onFocus="this.value='';" onBlur="if (this.value == '') {this.value = 'Email-id';}"required />
</div>
<div class="clearfix"> </div>
</div>
<textarea name="messaggio" cols="77" rows="6"onFocus="this.value='';" onBlur="if (this.value == '') {this.value = 'Message';}"required >Scrivi qui il tuo messaggio</textarea>
<div class="send ">
<input type="submit" class="botton" value="Invia" >
</div>
</form>
Codice PHP:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "mia email";
$email_subject = "Your email subject line";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['nome']) ||
!isset($_POST['email']) ||
!isset($_POST['messaggio'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['nome']; // required
$email_from = $_POST['email']; // required
$comments = $_POST['messaggio']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
Ecco cosa vorrei fare:
1- Quando clicco il bottone invia, invece di mandarmi sulla pagina php e visualizzarmi la scritta "
Thank you for contacting us. We will be in touch with you very soon.", vorrei rimanesse sul form ( il sito è un single pages con ancore. L'ancora in questione si chiama #contact) e che il value del bottone cambiasse in Email inviata.
2- I messaggi di errore non vorrei visualizzarli nella pagina php ma direttamente nel value di ogni input, quindi rimanere sul form.
Spero qualcuno possa darmi una mano.
Grazie mille
Buona giornata eb Buona pasqua.
Valentina