Exemple d'implémentation : Un formulaire de contact en 5 minutes
Un article de ReaxiaWiki.
L'ajout d'un formulaire de contact à un site se déroule en deux étapes :
- la mise en place du mot-clé dans une page,
- l'interprétation de ce mot-clé dans le code PHP.
[modifier]
Mise en place du mot-clé
Il faut ajouter le mot-clé [[FORMULAIRE_CONTACT]] dans un champ paragraphe d'un contenu structuré :
Pour que le contributeur ait une description de ce mot-clé dans la liste des mots-clés, il faut ajouter une ligne dans le fichier reaxia_conf/Reaxia_Conf.php :
$GLOBALS['conf']['keywords']['FORMULAIRE_CONTACT'] = "Affiche le formulaire de contact pour l'envoi d'un mail au webmaster";
[modifier]
Interprétation du mot-clé
Le code suivant est à ajouter dans la librairie traitant l'affichage des pages du site.
- Si
$_POSTest vide, le code remplace le mot-clé par le forumlaire de contact. - Si le formulaire a été soumis, un e-mail est envoyé et le mot-clé est remplacé par une phrase confirmant au visiteur l'envoi de son message.
$textBefore = '';
$textAfter = '';
if (FindKeyword($Texte_de_la_page, '[[FORMULAIRE_CONTACT]]', $textBefore, $textAfter))
{
$Texte_de_la_page = $textBefore;
if (isset($_POST['action']) && $_POST['action'] == 'send_mail')
{
// send mail
$name = trim(stripslashes($_POST['single_line_1']));
$email = trim(stripslashes($_POST['single_line_2']));
if (empty($name))
$from = $email;
else
$from = "$name <$email>";
$to = 'myaddress@mycompany.com';
$subject = trim(stripslashes($_POST['single_line_3']));
$headers = "From: $from\r\n";
$headers .= "To: $to\r\n";
$headers .= "Content-type: text/plain; charset=\"iso-8859-1\"";
$message = str_replace("\r", "", stripslashes($_POST['paragraph_1']));
$succes = mail($to, $subject, $message, $headers);
// display confirmation
if ($succes)
$Texte_de_la_page .= "Merci, votre message a bien été envoyé. Nous y répondrons dès que possible.";
else
$Texte_de_la_page .= "Erreur : votre message n'a pas été envoyé. Merci de réessayer plus tard.";
}
else
{
// form
$Texte_de_la_page .= GetFormContact();
}
$Texte_de_la_page .= $textAfter;
}
/**
* Get HTML for the contact form
* @return string HTML code
*/
function GetFormContact()
{
$html = '';
$html .= '<form name="new_contact" action="index.php" method="POST">' . "\n";
$html .= '<table border="0" cellpadding="0" cellspacing="4" width="100%">' . "\n";
$html .= '<tr>' . "\n";
$html .= ' <td width="100" align="right" nowrap>Nom : </td>' . "\n";
$html .= ' <td><input type="text" name="single_line_1" style="width: 100%;"></td>' . "\n";
$html .= '</tr>' . "\n";
$html .= '<tr>' . "\n";
$html .= ' <td width="100" align="right" nowrap>E-mail : </td>' . "\n";
$html .= ' <td><input type="text" name="single_line_2" style="width: 100%;"></td>' . "\n";
$html .= '</tr>' . "\n";
$html .= '<tr>' . "\n";
$html .= ' <td width="100" align="right" nowrap>Sujet : </td>' . "\n";
$html .= ' <td><input type="text" name="single_line_3" style="width: 100%;"></td>' . "\n";
$html .= '</tr>' . "\n";
$html .= '<tr>' . "\n";
$html .= ' <td width="100" align="right" nowrap>Message : </td>' . "\n";
$html .= ' <td><textarea name="paragraph_1" rows="6" style="width: 100%;"></textarea></td>' . "\n";
$html .= '</tr>' . "\n";
$html .= '</table>' . "\n";
$html .= '<div style="text-align: right; padding-right: 4px;"><input type="submit"></div>' . "\n";
$html .= '</form>' . "\n";
return $html;
}
[modifier]
