Formulaire contact problème message vérification champs

Parus75 Messages postés 3 Statut Membre -  
 Sébastien -
Bonjour,

J'essaie de faire un formulaire de contact pour mon site, tout simple, mais comme je débute en php j'ai un peu de mal. Le sujet a déjà été traité sur le site, j'ai trouvé pas mal de tutos mais je n'arrive toujours pas à voir où se trouve l'erreur. En gros, lorsque les champs sont vides ou l'adresse mail mal saisie, le message d'erreur (qui se trouve dans un span à la fin du formulaire) ne s'affiche pas - et rien d'autre ne se passe quand je clique sur le bouton "envoyer".

Merci d'avance pour votre aide !

Voici comment se présente le formulaire :



<div id="thanks" style="position:absolute; top:400px; left: 200px; font-family:'Bradley Hand ITC'; font-size:30px; color:#660000; font-weight:bolder;">

<?php echo $merci;?>

</div>


<div id="formulaire">
<form action="" method="post" enctype="multipart/form-data" id="formulaire_contact">

<table width="500" border="0" cellspacing="2" cellpadding="4" style="font-family:'Bradley Hand ITC'; font-size:20px; font-weight:bolder;">

<tr>
<td width="180" >NOM *</td>
<td width="180"> PRÉNOM </td>
</tr>

<tr>
<td><input name="nom" type="text" id="nom" value="<?php if(isset($_POST['nom'])) echo htmlspecialchars($_POST['nom']);?>" size="25" style="background-color:#FFFFFF; height:25px; border-radius:4px; border-color:#660000;" /></td>
<td><input name="prenom" type="text" id="prenom" value="<?php if(isset($_POST['prenom'])) echo htmlspecialchars($_POST['prenom']);?>" size="25" style="background-color:#FFFFFF; height:25px; border-radius:4px; border-color:#660000;" /></td>
</tr>

<tr>
<td > SOCIÉTÉ</td>
<td> EMAIL *</td>
</tr>

<tr>
<td ><input name="societe" type="text" id="societe" value="<?php if(isset($_POST['societe'])) echo htmlspecialchars($_POST['societe']);?>" size="25" style="background-color:#FFFFFF; height:25px; border-radius:4px; border-color:#660000;" /></td>
<td><input name="mail" type="text" id="mail" value="<?php if(isset($_POST['mail'])) echo htmlspecialchars($_POST['mail']);?>" size="25" style="background-color:#FFFFFF; height:25px; border-radius:4px; border-color:#660000;" /></td>
</tr>

<tr>
<td colspan="2" align="left">MESSAGE *</td>
</tr>

<tr>
<td colspan="2"> <textarea name="message" id="message" value="<?php if(isset($_POST['message'])) echo htmlspecialchars($_POST['message']);?>" style="width:430px; height:300px; border-radius:4px; border-color:#660000; background-color:#FFFFFF; color:#660000" rows="5"></textarea></td>
</tr>

<tr>
<td colspan="2" align="left"><input name="envoyer" id="envoyer" type="button" value="Envoyer" style="margin-top:10px; background-color:#FFFFFF; color:#660000; font-size: 16px; font-weight:bold; border-radius:5px; padding:8px"/></td>
</tr>
</table>
</form>

<span style="position:absolute; width:500px; left:500px; top:350px; font-family:'Bradley Hand ITC'; font-size:30px; font-weight:bold; font-style:italic; color:rgba(204,0,0,1);">
<?php echo $erreur;?>

</span>

</div>


Et voici la partie php que j'ai mise en en-tête sur la page :
<?php 

$erreur="";
$merci="";

if(isset($_POST["envoyer"])){
extract($_POST);

if(!empty($nom)AND!empty($mail)AND!empty($message)){
//vérification du champ "mail"
if(preg_match("#^[a-z0-9._-]+@[a-z0-9._-]{2,}\[a-z]{2,4}$#",$mail)){
$nom=htmlspecialchars(addslashes($nom));
$prenom=htmlspecialchars(addslashes($prenom));
$societe=htmlspecialchars(addslashes($societe));
$mail=htmlspecialchars(addslashes($mail));
$message=htmlspecialchars(addslashes($message));

//mise en forme du message que je vais recevoir
$destinataire="xxxxxx@gmail.com";
$sujet="Contact depuis mon site";
$entete="From:".$nom."\r\n".
"Reply-To:".$mail."\r\n".
"X-Mailer:PHP/" .phpversion();
mail($destinataire,$sujet,$message,$entete);

unset($_POST, $message, $nom, $mail);

}else{
$erreur="Adresse email invalide";
}

//message qui doit s'afficher si le mail est bien parti

if(mail($destinataire,$sujet,$message,$entete)){
$merci="<p>Je vous remercie pour votre message</br>et m'engage à revenir vers vous rapidement</p>";
}
}

else{
$erreur="Veuillez remplir tous les champs obligatoires*";
}
}
?>

A voir également:

1 réponse

Sébastien
 
Bonjour, dans votre formulaire vous avez mis input type button il faut remplacer button par submit.

Pour formulaire il faut utiliser submit.

Voici le code qui marche, chez moi:

<?php
$erreur="";
$merci="";

if(isset($_POST["envoyer"])){
extract($_POST);

if(!empty($nom)AND!empty($mail)AND!empty($message)){
//vérification du champ "mail"
if(preg_match("#^[a-z0-9._-]+@[a-z0-9._-]{2,}\[a-z]{2,4}$#",$mail)){
$nom=htmlspecialchars(addslashes($nom));
$prenom=htmlspecialchars(addslashes($prenom));
$societe=htmlspecialchars(addslashes($societe));
$mail=htmlspecialchars(addslashes($mail));
$message=htmlspecialchars(addslashes($message));

//mise en forme du message que je vais recevoir
$destinataire="***@***";
$sujet="Contact depuis mon site";
$entete="From:".$nom."\r\n".
"Reply-To:".$mail."\r\n".
"X-Mailer:PHP/" .phpversion();
mail($destinataire,$sujet,$message,$entete);

unset($_POST, $message, $nom, $mail);

}else{
$erreur="Adresse email invalide";
}

//message qui doit s'afficher si le mail est bien parti

if(mail($destinataire,$sujet,$message,$entete)){
$merci="<p>Je vous remercie pour votre message</br>et m'engage à revenir vers vous rapidement</p>";
}
}

else{
$erreur="Veuillez remplir tous les champs obligatoires*";
}
}
?>

<div id="thanks" style="position:absolute; top:400px; left: 200px; font-family:'Bradley Hand ITC'; font-size:30px; color:#660000; font-weight:bolder;">
<?php echo $merci;?>
</div>
<div id="formulaire">
<form action="" method="post" enctype="multipart/form-data" id="formulaire_contact">
<table width="500" border="0" cellspacing="2" cellpadding="4" style="font-family:'Bradley Hand ITC'; font-size:20px; font-weight:bolder;">
<tr>
<td width="180" >NOM *</td>
<td width="180"> PRÉNOM </td>
</tr>
<tr>
<td><input name="nom" type="text" id="nom" value="<?php if(isset($_POST['nom'])) echo htmlspecialchars($_POST['nom']);?>" size="25" style="background-color:#FFFFFF; height:25px; border-radius:4px; border-color:#660000;" /></td>
<td><input name="prenom" type="text" id="prenom" value="<?php if(isset($_POST['prenom'])) echo htmlspecialchars($_POST['prenom']);?>" size="25" style="background-color:#FFFFFF; height:25px; border-radius:4px; border-color:#660000;" /></td>
</tr>
<tr>
<td > SOCIÉTÉ</td>
<td> EMAIL *</td>
</tr>
<tr>
<td ><input name="societe" type="text" id="societe" value="<?php if(isset($_POST['societe'])) echo htmlspecialchars($_POST['societe']);?>" size="25" style="background-color:#FFFFFF; height:25px; border-radius:4px; border-color:#660000;" /></td>
<td><input name="mail" type="text" id="mail" value="<?php if(isset($_POST['mail'])) echo htmlspecialchars($_POST['mail']);?>" size="25" style="background-color:#FFFFFF; height:25px; border-radius:4px; border-color:#660000;" /></td>
</tr>
<tr>
<td colspan="2" align="left">MESSAGE *</td>
</tr>
<tr>
<td colspan="2"> <textarea name="message" id="message" value="<?php if(isset($_POST['message'])) echo htmlspecialchars($_POST['message']);?>" style="width:430px; height:300px; border-radius:4px; border-color:#660000; background-color:#FFFFFF; color:#660000" rows="5"></textarea></td>
</tr>
<tr>
<td colspan="2" align="left"><input name="envoyer" id="envoyer" type="submit" value="Envoyer" style="margin-top:10px; background-color:#FFFFFF; color:#660000; font-size: 16px; font-weight:bold; border-radius:5px; padding:8px"/></td>
</tr>
</table>
</form>
<span style="position:absolute; width:500px; left:500px; top:350px; font-family:'Bradley Hand ITC'; font-size:30px; font-weight:bold; font-style:italic; color:rgba(204,0,0,1);">
<?php echo $erreur;?>
</span>
</div>
0