Source: lesson_20_a0_envoyer_un_mail.php | Résultat |
<?php include("../style_sheets/style.php"); if (!isSet($_POST['uname'])) { //Construction du formulaire echo "<table CELLSPACING=0 BORDER=0 CELLPADDING=0>"; echo "<TR><TD COLSPAN=2 CLASS=header>Saisir les informations ci-après, puis cliquez sur 'ENVOYER'.</td></TR>"; //nom echo "<TR CLASS=Alt1><td>"; echo "<form action='../commons/show_exercises.php? exercise=lesson_20_a0_envoyer_un_mail.php' method=post>"; //echo "<form action=$_SERVER[PHP_SELF] method=post>"; echo "<b> Votre nom :</td>"; echo "<td><b><input type=text NAME=uname > </td> </TR><TR CLASS=Alt1></TR>" ; //prenom echo "<TR CLASS=Alt1><td>"; echo "<b> Votre prénom:</td>"; echo "<td><b><input type=text NAME=first_name> </td></TR><TR CLASS=Alt1></TR>" ; // echo "<TR CLASS=Alt1>"; echo "<td><b> Votre adresse email (indispensable) </td>"; echo "<td><b><input type=text NAME=e_mail size=20> </td></TR><TR CLASS=Alt1></TR>"; // //commentaires echo "<TR CLASS=Alt1><td>"; echo "<b> Votre demande :</td>"; echo "<td><textarea name='comments' cols=15 rows=4> </textarea> </TR>" ; echo "<tr CLASS=Alt1><TD COLSPAN=2><DIV align=RIGHT> <input type=submit NAME=Login value=ENVOYER > </DIV></td></tr>"; echo "</table><p> </p>"; } else { //Construction et envoi d'un email $mailTo = $_POST['e_mail'] ; $mailSubject = 'Test de messagerie' ; $mailBody = " Nom: ".$_POST['uname']."\r\n Prènom: ". $_POST['first_name']."\r\n E mail: ".$_POST['e_mail']. "\r\n Commentaires: ".$_POST['comments'] ; $mailHeaders = "From : PHP training\r\n" ; if (mail($mailTo, $mailSubject, $mailBody, $mailHeaders)) { echo "Votre message a bien été envoyé, contenu du message:<p>" ; echo "<table>" ; echo "<tr><td>Nom (patronyme) :</td> <td>".$_POST['uname']."</td></tr>"; echo "<tr><td>Prénom :</td> <td>".$_POST['first_name']."</td></tr>"; echo "<tr><td>e mail :</td> <td>".$_POST['e_mail']."</td></tr>"; echo "<tr><td>Votre demande :</td> <td>".$_POST['comments']."</td></tr></table>"; } else { echo "<br/>Désolé, il y a eu une erreur, le courrier n'a pas pu être envoyé <p>" ; }
} ?>
Fichier : FormValidator.class.inc
<?php
// FormValidator.class.inc // class to perform form validation
class validationFormulaire { // private variables var $_errorList; // methods (private) // function to get the value of a variable (field) function _getValue($field) { //echo $field."<br>"; global ${$field}; return ${$field}; } // methods (public) // constructor // reset error list function FormValidator() { $this->resetErrorList(); } // check whether input is empty function isEmpty($field, $msg) { $value = $this->_getValue($field); if (isSet($_POST[$field])) { switch (getType($_POST[$field])) { case "string": $value=$_POST[$field]; break; case "NULL": $value=""; break; case "array": $value=$_POST[$field][0]; break; } if (trim($value) == "") { $this->_errorList[] = array("field" => $field, "value" => $value, "msg" => $msg); return false; } else { return true; } } else { $this->_errorList[] = array("field" => $field, "value" => $value, "msg" => $msg); return false; } }
// check whether input is a string function isString($field, $msg) { //$value = $this->_getValue($field); $value = $_POST[$field] ; if(!is_string($value)) { $this->_errorList[] = array("field" => $field, "value" => $value, "msg" => $msg); return false; } else { return true; } }
function isDate($date_day,$date_month,$date_year, $msg) { //$value = $this->_getValue($field); $field = "Date"; $value = "Day:".$_POST[$date_day]." Month:". $_POST[$date_month]." Year:".$_POST[$date_year]; if(!checkdate($_POST[$date_month],$_POST[$date_day], $_POST[$date_year])) { $this->_errorList[] = array("field" => $field, "value" => $value, "msg" => $msg); return false; } else { return true; } }
// check whether input is a number function isNumber($field, $msg) { //$value = $this->_getValue($field); $value = $_POST[$field] ; if(!is_numeric($value)) { $this->_errorList[] = array("field" => $field, "value" => $value, "msg" => $msg); return false; } else { return true; } }
// check whether input is an integer function isInteger($field, $msg) { //$value = $this->_getValue($field); $value = $_POST[$field] ; if(!is_integer($value)) { $this->_errorList[] = array("field" => $field, "value" => $value, "msg" => $msg); return false; } else { return true; } }
// check whether input is a float function isFloat($field, $msg) { //$value = $this->_getValue($field); $value = $_POST[$field] ; if(!is_float($value)) { $this->_errorList[] = array("field" => $field, "value" => $value, "msg" => $msg); return false; } else { return true; } } // check whether input is alphabetic function isAlpha($field, $msg) { //$value = $this->_getValue($field); $value = $_POST[$field] ; $pattern = "/^[a-zA-Z]+$/"; if(preg_match($pattern, $value)) { return true; } else { $this->_errorList[] = array("field" => $field, "value" => $value, "msg" => $msg); return false; } }
// check whether input is within a valid numeric range function isWithinRange($field, $msg, $min, $max) { //$value = $this->_getValue($field); $value = $_POST[$field] ; if(!is_numeric($value) || $value < $min || $value > $max) { $this->_errorList[] = array("field" => $field, "value" => $value, "msg" => $msg); return false; } else { return true; } } // check whether input is a valid email address function isEmailAddress($field, $msg) { //$value = $this->_getValue($field); $value = $_POST[$field] ; $pattern = "/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*". "@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/"; if(preg_match($pattern, $value)) { return true; } else { $this->_errorList[] = array("field" => $field, "value" => $value, "msg" => $msg); return false; } } // return the current list of errors function getErrorList() { return $this->_errorList; } // check whether any errors have occurred in validation // returns Boolean function isError() { if (sizeof($this->_errorList) > 0) { return true; } else { return false; } }
// reset the error list function resetErrorList() { $this->_errorList = array(); } } ?>
|
|