SHARE
TWEET

Untitled

a guest Mar 22nd, 2019 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: wilder18
  5.  * Date: 17/03/19
  6.  * Time: 16:13
  7.  */
  8.  
  9. // define variables and set to empty values
  10. $nameErr = $emailErr = $telErr = $commentErr = $subjectErr = "";
  11. $name = $email = $tel = $comment = $subject = "";
  12. $errors= 0;
  13.  
  14. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  15.   if (empty($_POST["name"])) {
  16.     $nameErr = "Name is required";
  17.     $errors+= 1;
  18.  
  19.   } else {
  20.     $name = test_input($_POST["name"]);
  21.     // check if name only contains letters and whitespace
  22.     if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
  23.       $nameErr = "Only letters and white space allowed";
  24.     }
  25.   }
  26.  
  27.   if (empty($_POST["email"])) {
  28.     $emailErr = "Email is required";
  29.     $errors+= 1;
  30.  
  31.   } else {
  32.     $email = test_input($_POST["email"]);
  33.     // check if e-mail address is well-formed
  34.     if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  35.       $emailErr = "Invalid email format";
  36.     }
  37.   }
  38.  
  39.   if (empty($_POST["tel"])) {
  40.         $telErr = "Telephone number is required";
  41.         $errors+= 1;
  42.  
  43.   } else {
  44.         $tel = test_input($_POST["tel"]);
  45.   }
  46.  
  47.   if (empty($_POST["comment"])) {
  48.     $commentErr = "Comment is required";
  49.     $errors+= 1;
  50.  
  51.   } else {
  52.     $comment = test_input($_POST["comment"]);
  53.   }
  54.  
  55.   if (empty($_POST["subject"])) {
  56.     $subjectErr = "Subject is required";
  57.     $errors+= 1;
  58.  
  59.   } else {
  60.     $subject = test_input($_POST["subject"]);
  61.   }
  62.     if ($errors ==0){
  63.         echo "<h2> Message bien envoy√© !</h2>";
  64.     }
  65. }
  66.  
  67. function test_input($data) {
  68.   $data = trim($data);
  69.   $data = stripslashes($data);
  70.   $data = htmlspecialchars($data);
  71.   return $data;
  72. }
  73. ?>
  74.  
  75. <style>
  76.     .error {color: #FF0000;}
  77. </style>
  78.  
  79. <form  action=""  method="post">
  80.     <div>
  81.         <label  for="nom">Nom :</label>
  82.         <input  type="text"  id="nom"  name="name">
  83.         <span class="error">* <?= $nameErr;?></span>
  84.     </div>
  85.  
  86.     <div>
  87.         <label  for="courriel">Courriel :</label>
  88.         <input  type="email"  id="courriel"  name="email">
  89.         <span class="error">* <?= $emailErr;?></span>
  90.     </div>
  91.  
  92.     <div>
  93.         <label for="telephone">Telephone: </label>
  94.         <input type="tel" id="telephone" name="tel">
  95.         <span class="error">* <?= $telErr;?></span>
  96.     </div>
  97.  
  98.     <div>
  99.         <label for="subject_form"> Sujet: </label>
  100.         <select id="subject_form" name="subject">
  101.             <option value="subject1">subject1</option>
  102.             <option value="subject2">subject2</option>
  103.             <option value="subject3">subject3</option>
  104.             <option value="subject4">subject4</option>
  105.         </select>
  106.         <span class="error">* <?= $subjectErr;?></span>
  107.     </div>
  108.  
  109.     <div>
  110.         <label  for="comment">Message :</label>
  111.         <textarea  id="comment"  name="comment"></textarea>
  112.         <span class="error">* <?= $commentErr;?></span>
  113.     </div>
  114.  
  115.     <div  class="button">
  116.         <button  type="submit">Envoyer votre message</button>
  117.     </div>
  118. </form>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top