Dopo una settimana di malattia eccoci di nuovo a romperci la testa su sto form...
Innanzitutto grazie a tutti per avermi dato preziose indicazioni.
Ecco qui il codice sistemato e corretto dagli errori che mi avete fatto notare:
Codice PHP:
<?php
//If the form is submitted
if( isset($_POST['submit']) )
{
// NAME (requested) field validation
if (trim($_POST['name']) == '') {
$hasError = true;
} else if (!eregi("^[a-zA-Z]+$", trim($_POST['name']))) {
$hasError = true;
print "Errore name
";
} else {
$name = trim($_POST['name']);
}
// TELEPHONE (optional) field validation
if (!eregi("^[0-9]+$", trim($_POST['telephone']))) {
$hasError = true;
print "Errore telephone
";
} else {
$telephone = trim($_POST['telephone']);
}
// FAX (optional) field validation
if (!eregi("^[0-9]+$", trim($_POST['fax']))) {
$hasError = true;
print "Errore fax
";
} else {
$fax = trim($_POST['fax']);
}
// EMAIL (requested) field validation
if (trim($_POST['email']) == '') {
$hasError = true;
} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
$hasError = true;
print "Errore email
";
} else {
$email = trim($_POST['email']);
}
// ADDRESS (optional) field validation
if (!eregi("^[a-zA-Z0-9]+$", trim($_POST['address']))) {
$hasError = true;
print "Errore address
";
} else {
$address = trim($_POST['address']);
}
// CITY (optional) field validation
if (!eregi("^[a-zA-Z]+$", trim($_POST['city']))) {
$hasError = true;
print "Errore city
";
} else {
$city = trim($_POST['city']);
}
// Check to make sure MESSAGE (requested) were entered
if (trim($_POST['message']) == '') {
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$messages = stripslashes(trim($_POST['message']));
print "Errore message
";
} else {
$messages = trim($_POST['message']);
}
}
// If there is no error, send the email
if ( !$hasError ) {
$emailTo = 'mattia@weston.it'; //destination email address here
$body = "
Nome e Cognome: $name \n\n
Telefono: $telephone \n\n
Fax: $fax \n\n
Email: $email \n\n
Indirizzo: $address \n\n
Città: $city \n\n
Messaggio:\n $message
";
$headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="it" lang="it">
<head>
<title>form</title>
</head>
<body id="contatti" onload="initialize()">
<h3>Contact form</h3>
<?php if(isset($hasError)) { //If errors are found ?>
<p class="form-error" style="color:red">Messaggio di errore</p>
<?php } ?>
<?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
<p class="form-success" style="color:green">Email inviata correttamente</p>
<?php } ?>
<form id="contactform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label class="inlined" for="name">Nome e Cognome</label>
<input type="text" class="input-text validate[required,custom[onlyLetter],length[0,50]]" id="name" name="name" value="<?php echo $_POST['name'] ?>" />
<label class="inlined" for="telephone">Telefono (optional)</span></label>
<input type="text" class="input-text validate[optional,custom[onlyNumber],length[0,20]]" id="telephone" name="telephone" value="<?php echo $_POST['telephone'] ?>"/>
<label class="inlined" for="fax">Fax (opzionale)</span></label>
<input type="text" class="input-text validate[optional,custom[onlyNumber],length[0,20]]" id="fax" name="fax" value="<?php echo $_POST['fax'] ?>"/>
<label class="inlined" for="email">Email</label>
<input type="text" class="input-text validate[required,custom[email],length[0,50]]" id="email" name="email" value="<?php echo $_POST['email'] ?>"/>
<label class="inlined" for="address">Indirizzo (opzionale)</span></label>
<input type="text" class="input-text validate[optional,length[0,100]]" id="address" name="address" value="<?php echo $_POST['address'] ?>" />
<label class="inlined" for="city">Citta' (opzionale)</span></label>
<input type="text" class="input-text validate[optional,custom[onlyLetter],length[0,50]]" id="city" name="city" value="<?php echo $_POST['city'] ?>" />
<label class="textarea inlined" for="message">Messaggio</label>
<textarea class="input-text validate[required,length[6,300]]" id="message" name="message" rows="6" cols="150"><?php echo $_POST['message'] ?></textarea>
<input class="button" type="submit" value="invia" name="submit" />
</form>
</body>
</html>
Il problema è che non funziona ancora perchè non mi permette di inserire gli spazi né all'interno degli input text né all'interno della textarea: es. nel campo address non mi lascia inserire "via verdi" ma dovrei inserirlo senza spazio "viaverdi", solo così mi lascia inviare il messaggio senza ritornarmi errore.
Ecco a voi quindi il domandone finale:
come modificare i vari
Codice PHP:
eregi("^[a-zA-Z]+$")
Codice PHP:
eregi("^[0-9]+$")
Codice PHP:
eregi("^[a-zA-Z0-9]+$")
affinché mi si permetta di inserire delle parole "staccate" da spazi senza che il codice mi si incazzi?
Grazie di nuovo