如何为我的联系人添加PHP验证

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.

It's just a simple contact form:

First Name Last Name Email Message (textarea) Submit button

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.

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...

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...

Here is the html contact form I made.

<form action="message_sent.php" method="POST"      onSubmit="return validateTextbox();">

<p class="message">
<div class="row">

<label for="firstname"><!--First Name (Required):-->    </label>
<input type="text" id="firstname" name="first_name"  size="40" placeholder="First Name (Required)"></br> </br>   </div>


<div class="row">

<label for="lastname"><!--Last Name (Required):--> </label>
<input type="text" id="lastname" name="last_name"   size="40" placeholder="Last Name (Required)"/> </br>   </br></div>

<div class="row">
<label for="email"><!--Your Email (Required):--></label>
<input type="email" id="email" name="email" size="40"  placeholder="Email (Required)"/> </br> </br></div>

<!--<p class="yourMessage">Your Message (10 Character Minimum):</p>--> 
<textarea id="theform" rows="30" cols="80"  name="message" placeholder="Your Message (10 Character Minimum):"></textarea></br>

</p>

<input type="submit" name="submit" onclick="return val();"  value="SUBMIT">

</form>

</body>
</html>

Here is the javascript validation:

/*============================ 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 < 1 || box2.value.length < 1 ||  box3.value.length < 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 < 10 
        && document.getElementById("theform").value.length > 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 < 70) ================================================ */


   /*========= CONTACT  PAGE========================================*/

    /*
 function contactPage(){


alert("This Contact Form is NOT OPERATIONAL.");

Here is the php submission success page... the code I used to have the form send to my email:

<?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: 

 
First Name: $first_name
 
Last Name: $last_name

Email: $email


MESSAGE: $message";

mail($to, $subject, $body);

print "<p>Message Sent! <a href='index.html'>Click.       here</a> to return to the homepage</p>"


?>

This is just a basic empty check validation.

Validations in PHP are not very difficult.

if ($_SERVER["REQUEST_METHOD"] == "POST") {
   if(trim($_POST['firstname']) === ''){
      echo 'Please enter firstname';
   }
}

You can also learn some validations here http://www.w3schools.com/php/php_form_validation.asp

Simple and complete example:

<!DOCTYPE HTML>  
<html>
    <head>
        <style>
            .error {color: #FF0000;}
        </style>
    </head>
    <body>
        <?php

            //Define variable as empty to avoid "undefined variable error".
            $FnameErr = $LnameErr = $emailErr = ""; //Each variable contain error string of specific field.
            $fname = $lname = $email = $message = ""; //Main inputed data of specific field


            if ($_SERVER["REQUEST_METHOD"] == "POST") { //check if request is posted.

              //First name check
              if (empty($_POST["fname"])) {            // check if empty then assign a error string in spacific variable
                $FnameErr = "First Name is required";
              }else{                                   // if not empty then store as right data
                $fname = filter_data($_POST["fname"]); //filter with unexpected special char and trim.
              }

              //Last name
              if (empty($_POST["lname"])) {            // check if empty then assign a error string in spacific variable
                $LnameErr = "Last Name is required";  
              }else{                                   // if not empty then store as right data
                $lname = filter_data($_POST["lname"]); //filter with unexpected special char and trim.
              }

              //email
              if (empty($_POST["email"])) {            // check if empty then assign a error string in spacific variable
                $emailErr = "Email is required"; 
              } else {
                if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {  // validate email with php build-in fucntion.
                  $emailErr = "Invalid email format"; 
                }else{                                             // if not empty and valide email then store as right data
                  $email = filter_data($_POST["email"]);           //filter with unexpected special char and trim.
                }
              }

              //message with no validation
              if (empty($_POST["message"])) {
                $message = "";
              } else {
                $message = filter_data($_POST["message"]);
              }


              //Database query
              if($FnameErr =="" && $LnameErr =="" && $emailErr==""){
                //MYSQL insert statement that you know
                // $sql =  "INSERT INTO tablename .................."; values = $fname; $lname; $email; $message;
              }

            }


            // A function to trim data and remove special charecter
            function filter_data($data) {
              $data = trim($data);
              $data = stripslashes($data);
              $data = htmlspecialchars($data);
              return $data;
            }
          ?>


        <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">  
            First Name: <input type="text" name="fname" value="">
            <span class="error">* <?php echo $FnameErr;?></span><br><br>

            Last Name: <input type="text" name="lname" value="">
            <span class="error">* <?php echo $LnameErr;?></span><br><br>

            E-mail: <input type="text" name="email" value="">
            <span class="error">* <?php echo $emailErr;?></span>
            <br><br>

            Message: <textarea name="message" rows="5" cols="40"></textarea>
            <br><br>

            <input type="submit" name="submit" value="Submit">  
        </form>

        <?php
          echo "<h2>Your Input:</h2>";
          echo $fname;
          echo "<br>";

          echo $lname;
          echo "<br>";
          echo $email;
          echo "<br>";
          echo $message;
        ?>
    </body>
</html>