PHP formulaire de contact

Ginolattera Messages postés 26 Statut Membre -  
 Ginolattera -
Bonjour,
j'ai un script de formulaire de contact avec boutons radio, j'ai réussi l'envoyer et recevoir,mais la réception n'a pas de boutons de radio ( madame, monsieur). le wamp serveur est indiqué l'erreur: Undefined index: action in C:\wampserver32\www\test\index.php on line 2.
j'espère qq'un pourra m'expliquer comment modifier mon script comme ceci pour que je puisse recevoir les boutons radio:

1.<?php
2. $action=$_REQUEST['action'];
3.if ($action=="") /* display the contact form */
4. {
5. ?>
6. <form action="" method="POST" enctype="multipart/form-data">
7. <input type="hidden" name="action" value="submit">
8. <input type="radio" name="veg" value="cabbage" <?php if(!isset($veg)){print "
9. checked=\"checked\"";} if(isset($veg) && $veg == "cabbage"){print " 10.checked=\"checked\"";} ?>> Madame
11. <input type="radio" name="veg" value="onion" <?php if(isset($veg) && $veg == "onion"){print " checked=\"checked\"";} ?>> Monsieur <br>

12 Votre nom et prénom:<br>
13. <input name="name" type="text" value="" size="40"/><br>
14. Votre email:<br>
15. <input name="email" type="text" value="" size="40"/><br>
16. Votre adresse:<br>
17. <input name="message" type="text" value="" size="80"/><br>
18. <input type="submit" value="envoyer"/>
19. </form>
20. <?php
21. }
22. else /* send the submitted data */
23. {
24. $name=$_REQUEST['name'];
25. $email=$_REQUEST['email'];
26. $message=$_REQUEST['message'];
27. if (($name=="")||($email=="")||($message==""))
28. {
29. echo "Veuillez remplir tous les cases, <a href=\"\">the form</a> remplissez encore 30.une fois SVP!.";
31. }
32. else{
33. $from="From: $name<$email>\r\nReturn-path: $email";
34. $subject="Message sent using your contact form";
35. mail("1234@club-internet.fr", $subject, $message, $from);
36. echo "Email est parti!";
37. }
38. }
39. ?>

4 réponses

  1. Reivax962 Messages postés 3742 Statut Membre 1 011
     
    Bonjour,

    Ton message d'erreur provient de ta ligne
    $action=$_REQUEST['action'];
    qui est appelée lors du chargement de la page : 'action' ne sera défini que lorsque le formulaire sera validé. Donc lors du premier appel, il n'est pas défini, d'où le message.

    Pour la coche Monsieur / Madame, le problème vient également d'une définition de variable : $veg n'est jamais défini lorsqu'on y vient.

    Je te suggère la correction suivante :
    <?php
    $action = (isset($_REQUEST['action'])) ? $_REQUEST['action'] : '';
    $veg = (isset($_REQUEST['veg'])) ? $_REQUEST['veg'] : 'cabbage';
    if ($action=="") /* display the contact form */
    {
    ?>
    <form action="" method="POST" enctype="multipart/form-data">
    	<input type="hidden" name="action" value="submit">
    	<input type="radio" name="veg" value="cabbage" <?php if($veg == "cabbage"){print " checked=\"checked\"";} ?>> Madame
    	<input type="radio" name="veg" value="onion" <?php if($veg == "onion"){print " checked=\"checked\"";} ?>> Monsieur <br>
    	Votre nom et prénom:<br>
    	<input name="name" type="text" value="" size="40"/><br>
    	Votre email:<br>
    	<input name="email" type="text" value="" size="40"/><br>
    	Votre adresse:<br>
    	<input name="message" type="text" value="" size="80"/><br>
    	<input type="submit" value="envoyer"/>
    </form>
    <?php
    }
    else /* send the submitted data */
    {
    	$name=$_REQUEST['name'];
    	$email=$_REQUEST['email'];
    	$message=$_REQUEST['message'];
    	if (($name=="")||($email=="")||($message==""))
    	{
    		echo "Veuillez remplir tous les cases, <a href=\"\">the form</a> remplissez encore une fois SVP!.";
    	}
    	else{
    		$from="From: $name<$email>\r\nReturn-path: $email";
    		$subject="Message sent using your contact form";
    		mail("1234@club-internet.fr", $subject, $message, $from);
    		echo "Email est parti!";
    	}
    }
    ?>
    1
  2. Ginolattera
     
    bonjour,
    merci pour votre réponse, ça marche toujours pas,
    mais j'ai trouvé la solution
    0
  3. linkje Messages postés 128 Statut Membre 1
     
    Ecrit la nous ? :)
    0
  4. Ginolattera
     
    voici les scriptes, il marche bien:

    index.htm
    <html>
    <body>

    <p>Formulaire </p>

    <form action="contact.php" method="post">
    <p>Votre sexe?
    <input type="radio" name="likeit" value="mademoiselle" checked="checked" /> Mademoiselle
    <input type="radio" name="likeit" value="madame" /> Madame
    <input type="radio" name="likeit" value="monsieur" /> Monsieur</p>
    <p><b>Votre nom et prénom:</b> <input type="text" name="yourname" size="40" /><br />
    <b>Subject:</b> <input type="text" name="subject" size="40" /><br />
    <b>E-mail:</b> <input type="text" name="email" size="40" /><br />
    Website: <input type="text" name="website"size="40" /></p>

    <p><b>Votre adresse:</b><br />
    <input type="text" name="adresse" size="100" /></p>

    <select name="how">
    <option value=""> -- Pays -- </option>
    <option>FRANCE</option>
    <option>BELGIQUE</option>
    <option>SUISSE</option>
    <option>LUXEMBOURG</option>
    <option>AUTRE</option>
    </select>

    <p><input type="submit" value="Envoyer!"></p>

    </form>

    </body>
    </html>

    contact.php

    <?php
    /* Set e-mail recipient */
    $myemail = "***@***";

    /* Check all form inputs using check_input function */
    $yourname = check_input($_POST['yourname'], "Enter your name");
    $subject = check_input($_POST['subject'], "Write a subject");
    $email = check_input($_POST['email']);
    $website = check_input($_POST['website']);
    $likeit = check_input($_POST['likeit']);
    $how_find = check_input($_POST['how']);
    $adresse = check_input($_POST['adresse'], "Write your comments");

    /* If e-mail is not valid show error message */
    if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
    {
    show_error("E-mail address not valid");
    }

    /* If URL is not valid set $website to empty */
    if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website))
    {
    $website = '';
    }

    /* Let's prepare the message for the e-mail */
    $message = "Hello!

    $likeit
    Name: $yourname
    E-mail: $email
    URL: $website
    Adresse:$adresse
    $how_find
    End of message
    ";

    /* Send the message using mail() function */
    mail($myemail, $subject, $message);

    /* Redirect visitor to the thank you page */
    header('Location: thanks.htm');
    exit();

    /* Functions we used */
    function check_input($data, $problem='')
    {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
    show_error($problem);
    }
    return $data;
    }

    function show_error($myError)
    {
    ?>
    <html>
    <body>

    <b>Veuillez corriger les erreurs:</b><br />
    <?php echo $myError; ?>

    </body>
    </html>
    <?php
    exit();
    }
    ?>

    thank.htm

    <html>
    <body>

    <p><b>Votre message a été envoyé! </b></p>

    </body>
    </html>
    0