Validazione dei dati in PHP – Parte 2 di 2

Posted on febbraio 25, 2009
Filed Under Form Email | 13 Comments

Dopo aver compreso la funzionalita’ di filter_var realizziamo un semplice form con validazione dei dati ed invio di una mail di conferma che includera’:
– nome
– email
– Home Page
– Messaggio

Una volta che tutti i dati saranno stati corretti e validati verra’ inviata una mail di conferma all’indirizzo inserito.

Il primo passo e’ la creazione del form:
<form name=”modulo” method=”post”>
Nome: <br/>
<input type=”text” name=”nome” value=”<?php echo $nome; ?>” size=”50″ /><br/><br/>
Email: <br/>
<input type=”text” name=”email” value=”<?php echo $email; ?>” size=”50″/> <br/><br/>
Home Page: <br/>
<input type=”text” name=”url” value=”<?php echo $url; ?>” size=”50″ /> <br/><br/>
Messaggio: <br/>
<textarea name=”msg” rows=”5″ cols=”50″><?php echo $msg; ?></textarea>
<br/>
<input type=”submit” name=”Submit” />
</form>

In testa al form dovra’ essere inserito il seguente codice PHP:
<?php
$errori = “”;    // Ci verranno inseriti gli errori da mostrare a video…

isset($_POST[‘nome’]) ? $nome = $_POST[‘nome’] : $nome = “”;
isset($_POST[‘email’]) ? $email = $_POST[‘email’] : $email = “”;
isset($_POST[‘url’]) ? $url = $_POST[‘url’] : $url = “”;
isset($_POST[‘msg’]) ? $msg = $_POST[‘msg’] : $msg = “”;

if (strlen($nome)>0)
$nome = filter_var($nome, FILTER_SANITIZE_STRING);
if ($nome == “”)
$errori .= ‘Inserire un nome valido.<br/><br/>’;

if (strlen($email)>0)
{
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
if (!filter_var($email, FILTER_VALIDATE_EMAIL))
$errori .= “$email non e’ un indirizzo mail valido<br/><br/>”;
}
if ($email == “”)
$errori .= “Indirizzo mail non valido o non presente<br/><br/>”;

if (strlen($url)>0)
{
$url = filter_var($url, FILTER_SANITIZE_URL);
if (!filter_var($url, FILTER_VALIDATE_URL))
$errori .= “$url non e’ un URL valido<br/><br/>”;
}
if ($url == “”)
$errori .= “URL non presente o non valido<br/><br/>”;

if (strlen($msg)>0)
$msg = filter_var($msg, FILTER_SANITIZE_STRING);
if ($msg == “”)
$errori .= ‘Inserire il messaggio da inviare.<br/><br/>’;

// Controllo gli errori…
if (strlen($errori) > 0)
echo ‘<div style=”color: red”>’ . $errori . ‘<br/></div>’;
else
{
$destinatario = ‘info@freescriptphp.com’;    // Da cambiare in base alle esigenze
$subject = ‘Mail dal form’;
$message  = ‘From: ‘.$nome.”\n”;
$message .= ‘Email: ‘.$email.”\n”;
$message .= ‘Homepage: ‘.$url.”\n”;
$message .= “Message:\n”.$msg.”\n\n”;
mail($destinatario, $subject, $message);
}
?>

Others Script adv

13 Responses to “Validazione dei dati in PHP – Parte 2 di 2”

  1. Arnita Martelli on febbraio 25th, 2011 22:06

    This is the best technique to be followed, I must say that really being here and not using this will really be silly. Will you please reply with more information here? I think you have a good response and it will definitely help a lot. As this forum has really been a good platform for us all, contributing to this will really be healthy for our and your business as well. We all are waiting for a good informative response. Thanks a lot for this, waiting for more.

  2. Kira Gleiss on febbraio 27th, 2011 00:01

    Thank you on your assist!

  3. personal bankruptcy lawyer on febbraio 27th, 2011 02:09

    Awesome post, are you on facebook?

  4. fx trading rates on marzo 2nd, 2011 04:45

    Great blog here! Also your web site loads up very fast! What host are you using? Can I get your affiliate link to your host? I wish my site loaded up as quickly as yours lol

  5. Ellan Cherrie on marzo 6th, 2011 21:32

    some really nice and useful info on this internet site , as well I conceive the style holds great features.

  6. motorcycle fairings on marzo 7th, 2011 22:14

    *Nice post. I learn something more challenging on different blogs everyday. It will always be stimulating to read content from other writers and practice a little something from their store. I’d prefer to use some with the content on my blog whether you don’t mind. Natually I’ll give you a link on your web blog. Thanks for sharing.

  7. et rid of duplicates in itunes on marzo 16th, 2011 08:11

    I am pleased to let you know your website has been selected for http://www.interesting-blogs.info . This website is now featuring on our homepage.

  8. leofire on marzo 18th, 2011 11:04

    Thanks a lots for your listing and have a good day.

  9. leofire on marzo 18th, 2011 11:20

    Actually we are using the dedicated hosting.

  10. leofire on marzo 18th, 2011 11:59

    No, I don’t like FB.

  11. leofire on marzo 18th, 2011 12:01

    Mail sent.

  12. Bobbie Gabourel on luglio 18th, 2011 04:05

    Hey there just wanted to give you a quick heads up. The words in your article seem to be running off the screen in Chrome. I’m not sure if this is a formatting issue or something to do with internet browser compatibility but I thought I’d post to let you know. The layout look great though! Hope you get the issue solved soon. Kudos

  13. leofire on settembre 16th, 2011 10:35

    Hi Bobbie,

    thanks a lots for your check; I have update wordpress and now it seems that everything works fine..is it ok also for you ?
    Have a good day,

    Leonardo

Leave a Reply