title: How do I add PHP validation to my contact for
body: <p>I made a website using css, html, javascript and a little php. I have it hosted on a hosting site with a domain name, but I'm having issue with the server side validation for the form.</p>
  
  <p>It's just a simple contact form:</p>
  
  <p>First Name
  Last Name
  Email
  Message (textarea)
  Submit button</p>
  
  <p>I have javascript validation and it works fine. The form won't submit at all unless the correct amount of characters are input, but obviously that's useless if the user has js disabled. I've also finally gotten the form to email to my personal email using php... But obviously I need server side validation as well.</p>
  
  <p>Today was my first day really getting into php and I was hoping to have the php validation done by today, but of all the videos I watched, I just came away more confused. All I want to do is just make it so that if the user has javascript disabled for whatever reason, or if he leaves the fields blank, the form won't submit...</p>
  
  <p>Anyeay, I was working on this all day today with the intention of getting the form to send to my email, I finally accomplished that. Then realized I had no server side validation. I spent a few hours researching it and attempting it to no avail. Can anyone here just give me the php validation code for the kind of form I described above? It's for my business website so the sooner I can have a working contact form on it, the better...</p>
  
  <p>Here is the html contact form I made.</p>
  
  <pre><code>&lt;form action="message_sent.php" method="POST"      onSubmit="return validateTextbox();"&gt;
  
  &lt;p class="message"&gt;
  &lt;div class="row"&gt;
  
  &lt;label for="firstname"&gt;&lt;!--First Name (Required):--&gt;    &lt;/label&gt;
  &lt;input type="text" id="firstname" name="first_name"  size="40" placeholder="First Name (Required)"&gt;&lt;/br&gt; &lt;/br&gt;   &lt;/div&gt;
  
  
  &lt;div class="row"&gt;
  
  &lt;label for="lastname"&gt;&lt;!--Last Name (Required):--&gt; &lt;/label&gt;
  &lt;input type="text" id="lastname" name="last_name"   size="40" placeholder="Last Name (Required)"/&gt; &lt;/br&gt;   &lt;/br&gt;&lt;/div&gt;
  
  &lt;div class="row"&gt;
  &lt;label for="email"&gt;&lt;!--Your Email (Required):--&gt;&lt;/label&gt;
  &lt;input type="email" id="email" name="email" size="40"  placeholder="Email (Required)"/&gt; &lt;/br&gt; &lt;/br&gt;&lt;/div&gt;
  
  &lt;!--&lt;p class="yourMessage"&gt;Your Message (10 Character Minimum):&lt;/p&gt;--&gt; 
  &lt;textarea id="theform" rows="30" cols="80"  name="message" placeholder="Your Message (10 Character Minimum):"&gt;&lt;/textarea&gt;&lt;/br&gt;
  
  &lt;/p&gt;
  
  &lt;input type="submit" name="submit" onclick="return val();"  value="SUBMIT"&gt;
  
  &lt;/form&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Here is the javascript validation:</p>
  
  <pre><code>/*============================ CONTACT US PAGE    ==========================*/
  
  function validateTextbox() {
  
  
  var box = document.getElementById("firstname");
  var box2 = document.getElementById("lastname");
  var box3 = document.getElementById("email");
  var box4 = document.getElementById("theForm");
  
  
  
  if (box.value.length &lt; 1 || box2.value.length &lt; 1 ||  box3.value.length &lt; 5){
  alert("You must enter a value");    
  
  box.focus();
  box.style.border = "solid 3px red";
     box2.focus();
     box2.style.border = "solid 3px red";
        box3.focus();
        box3.style.border = "solid 3px red";
  
  
      return false;
      }
  
  
  }
  
  
  
          function val(){
  
              if(document.getElementById("theform").value.length &lt; 10 
          &amp;&amp; document.getElementById("theform").value.length &gt; 0){
  
              alert("You must enter at least 50  characters. Tell us what you need so we can better assist you.");
              return false;
  
          }
  
  
          else  if(document.getElementById("theform").value.length === 0){
  
              alert("You cannot submit an empty form.");
              theform.focus();
              theform.style.border = "solid 3px red";
              return false;
          } 
  
  
  
      }
  
  
              /*function val(){
  
           if(document.getElementById("theform").value==null || document.getElementById("theform").value==""){
              alert("The Message field cannot be blank.");
  
              return false;                                               
      }
  
  
          */
  
  
  
  
      /*
  
  
  
  */
  
  
  
  
   /*=======================|| box4.value.length &lt; 70) ================================================ */
  
  
     /*========= CONTACT  PAGE========================================*/
  
      /*
   function contactPage(){
  
  
  alert("This Contact Form is NOT OPERATIONAL.");
  </code></pre>
  
  <p>Here is the php submission success page... the code I used to have the form send to my email:</p>
  
  <pre><code>&lt;?php
  
  
  
  $first_name=$_POST['first_name'];
  $last_name=$_POST['last_name'];
  $email=$_POST['email'];
  $message=$_POST['message'];
  
  $to="default@email.com";
  $subject="A visitor has sent you a new message";
  $body="You have received a message from: \n\n 
  First Name: $first_name\n 
  Last Name: $last_name\n
  Email: $email\n\n
  MESSAGE: $message";
  
  mail($to, $subject, $body);
  
  print "&lt;p&gt;Message Sent! &lt;a href='index.html'&gt;Click.       here&lt;/a&gt; to return to the homepage&lt;/p&gt;"
  
  
  ?&gt;
  </code></pre>