title: Contact Form only sending message body but ignoring other fields
body: <p>Please guys am in deep trouble, i have being sweating hard for the past 24 hours, i have this contact form which i uploaded to my server, but the sad part is that, it is only sending the message body to my email, ignoring the other fields like name field, email field and phone number. I am tired of staring at the php code, i feel everything is ok but the code is not working as expected, please help me.</p>
  
  <p>Here is my php code:</p>
  
  <pre><code>&lt;?php 
  
  // define variables and set to empty values
  $name_error = $email_error = $phone_error = $url_error = "";
  $name = $email = $phone = $message = $url = $success = "";
  
  //form is submitted with POST method
  if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["name"])) {
      $name_error = "Name is required";
    } else {
      $name = test_input($_POST["name"]);
      // check if name only contains letters and whitespace
      if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
        $name_error = "Only letters and white space allowed"; 
      }
    }
  
    if (empty($_POST["email"])) {
      $email_error = "Email is required";
    } else {
      $email = test_input($_POST["email"]);
      // check if e-mail address is well-formed
      if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $email_error = "Invalid email format"; 
      }
    }
  
    if (empty($_POST["phone"])) {
      $phone_error = "Phone is required";
    } else {
      $phone = test_input($_POST["phone"]);
      // check if e-mail address is well-formed
      if (!preg_match("/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i",$phone)) {
        $phone_error = "Invalid phone number"; 
      }
    }
  
    if (empty($_POST["url"])) {
      $url_error = "";
    } else {
      $url = test_input($_POST["url"]);
      // check if URL address syntax is valid (this regular expression also allows dashes in the URL)
      if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&amp;@#\/%?=~_|!:,.;]*[-a-z0-9+&amp;@#\/%=~_|]/i",$url)) {
        $url_error = "Invalid URL"; 
      }
    }
  
    if (empty($_POST["message"])) {
      $message = "";
    } else {
      $message = test_input($_POST["message"]);
    }
  
    if ($name_error == '' and $email_error == '' and $phone_error == '' and $url_error == '' ){
        $message_body = '';
        unset($_POST['submit']);
        foreach ($_POST as $key =&gt; $value){
            $message_body .=  "$key: $value\n";
        }
  
        $to = 'emmanuelgbnn23@gmail.com';
        $subject = 'Contact Form Submit';
        if (mail($to, $subject, $message)){
            $success = "Message sent, thank you for contacting us!";
            $name = $email = $phone = $message = $url = '';
        }
    }
  
  }
  
  function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
  }
  </code></pre>
  
  <p>Here is my html code:</p>
  
  <pre><code>&lt;?php include('form_process.php'); ?&gt;
  &lt;link rel="stylesheet" href="form.css" type="text/css"&gt;
  &lt;div class="container"&gt;  
    &lt;form id="contact" action="&lt;?= htmlspecialchars($_SERVER["PHP_SELF"]) ?&gt;" method="post"&gt;
      &lt;h3&gt;Contact&lt;/h3&gt;
      &lt;h4&gt;Contact us today, and get reply with in 24 hours!&lt;/h4&gt;
      &lt;fieldset&gt;
        &lt;input placeholder="Your name" type="text" name="name" value="&lt;?= $name ?&gt;" tabindex="1" autofocus&gt;
        &lt;span class="error"&gt;&lt;?= $name_error ?&gt;&lt;/span&gt;
      &lt;/fieldset&gt;
      &lt;fieldset&gt;
        &lt;input placeholder="Your Email Address" type="text" name="email" value="&lt;?= $email ?&gt;" tabindex="2"&gt;
        &lt;span class="error"&gt;&lt;?= $email_error ?&gt;&lt;/span&gt;
      &lt;/fieldset&gt;
      &lt;fieldset&gt;
        &lt;input placeholder="Your Phone Number" type="text" name="phone" value="&lt;?= $phone ?&gt;" tabindex="3"&gt;
        &lt;span class="error"&gt;&lt;?= $phone_error ?&gt;&lt;/span&gt;
      &lt;/fieldset&gt;
      &lt;fieldset&gt;
        &lt;input placeholder="Your Web Site starts with http://" type="text" name="url" value="&lt;?= $url ?&gt;" tabindex="4" &gt;
        &lt;span class="error"&gt;&lt;?= $url_error ?&gt;&lt;/span&gt;
      &lt;/fieldset&gt;
      &lt;fieldset&gt;
        &lt;textarea value="&lt;?= $message ?&gt;" name="message" tabindex="5"&gt;
        &lt;/textarea&gt;
      &lt;/fieldset&gt;
      &lt;fieldset&gt;
        &lt;button name="submit" type="submit" id="contact-submit" data-submit="...Sending"&gt;Submit&lt;/button&gt;
      &lt;/fieldset&gt;
      &lt;div class="success"&gt;&lt;?= $success ?&gt;&lt;/div&gt;
    &lt;/form&gt;
  &lt;/div&gt;
  </code></pre>