PHP form validation code example – PHP contact form

Are you looking for simple php form validation example for a very beginner level then i think this post is very much useful for you. This example will show how to process PHP forms with security in mind. Proper validation of form data is important to protect your form from hackers and spammers!. You can free download simple php form validation example script using PHP from here. Hope this simple php form validation using PHP tutorial will help you to get the basic knowledge about the form validation in php as a beginner.
Take a quick look at this live demo to understand the flow of the simple php form validation example using php. Also you can download this simple php submit form or php contact form with validation script from here and can try in your localhost or live server where you want to do.

Learn php form validation step by step

Notice that at the start of the script, we check whether the form has been submitted using $_SERVER[“REQUEST_METHOD”]. If the REQUEST_METHOD is POST, then the form has been submitted – and it should be validated. So, from this php form validation script or php form validation code you are aware about email verification, validate email address, email validation in php. If it has not been submitted, skip the validation and display a blank form.

However, in the example above, Name and email input fields are require. The next step is to make input fields required and create error messages if needed. Also used here Name and email checking. Name should be alphabetical character and email should be a valid email for proper understanding of php form validation and how it’s work.

If user input pass all the validation checkpoint then form successfully submitted. Let’s go with the live example step by step.

//basic php contact form with some require fields..

<form class=“form-horizontal” method=“post”>
                    <fieldset>
                        <legend class=“text-center header” style=“width:70%;”>Contact us<span><img src=“images/logo1.png”></span></legend>

                        <div class=“form-group”>
                           
                            <div class=“col-md-8”>

//name field should be alphabetical and required if not fulfill the condition then show an error message...

                            <span style=“color:red;”><?php echo @$name_error;?></span>
                                <input id=“name” name=“name” type=“text” placeholder=“Name*” class=“form-control”>
                            </div>
                        </div>
                        <div class=“form-group”>
                            
                            <div class=“col-md-8”>

//email field should be valid email and required if not fulfill the condition then show an error message..

                            <span style=“color:red;”><?php echo @$email_error;?></span>
                                <input id=“email” name=“email” type=“text” placeholder=“Email Address*” class=“form-control”>
                            </div>
                        </div>
                        <div class=“form-group”>
                          
                            <div class=“col-md-8”>

//it's an optional field user can fill it or leave it blank...

                                <textarea class=“form-control” id=“message” name=“message” placeholder=“Enter your massage for us here.” rows=“7”></textarea>
                            </div>
                        </div>

                        <div class=“form-group”>
                            <div class=“col-md-12 text-center”>

//submit button for this form..

                                <button type=“submit” class=“btn btn-primary btn-lg”>Submit</button>
                            </div>
                        </div>
                    </fieldset>
                </form>

Now we have to create the validation. Below describe the code for php submit form validation using php.

<?php

//just make a function for reuse you can directly use this..

function check_user_input($input_data) {
   $output = trim($input_data); //trim the input value
   $output = stripslashes($output); //The stripslashes() function removes backslashes
   $output = htmlspecialchars($output); //The htmlspecialchars() function converts some predefined characters to HTML entities.
   return $output; // return the result
}

//checking the server request post or not if post then proceed...

if ($_SERVER["REQUEST_METHOD"] == "POST") {
	if($_POST["name"])
	{

// here we clean the input value and remove if any space, backslashes, or predefined characters 

	$name = check_user_input($_POST["name"]);

//after that check the name field value contain only alphabetical character and space or not. If yes then ok otherwise generate error message.

	if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
	  $name_error = "Only letters and white space allowed&quot";
	}	
	}
	else{
		$name_error = "Name field is required*";
	}
	if($_POST["email"])
	{

// same checking with email field value..

	$email = check_user_input($_POST["email"]);
	if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
	  $email_error = "Invalid email format";
	}	
	}
	else{
		$email_error = "Email field is required*";
	}
	if($name_error=="" && $email_error=="")
	{

// if nothing found in error message then receive the field value after submit and we can do anything with this data.

$name=$_POST["name"];
$email=$_POST["email"];
$msg=$_POST["message"];
	}
}
?>

In this way we can protect XSS or sql injection in the php contact form and make it secure. So basically if your query about contact form php, php validation, form validation php, php check valid email like these i think this php form validation example will be very helpful for you. So, don’t miss to learn and practice yourself. Thanks for watching this tutorial. You can try yourself and share with your friends. I will share some useful script in PHP as well as some tips and tricks in PHP programming. Viewers can subscribe our newsletter to get all the updates about web programming. Share and likes will be appreciated also help to evaluate our self.
Support
If you need any help regarding its configuration please feel free to comment we love to help you.

7 responses to “PHP form validation code example – PHP contact form”

  1. Sunny says:

    Maybe you could write next articles referring to this article. I want to read even more things about it!

  2. Borse says:

    Just a quick note to let you know that I appreciated your insights about the topic. Quite helpful for what I am interested in these days.

  3. BeataSMataka says:

    It’s a pity you don’t have got a donate button! I’d definitely donate for this excellent blog!

    I suppose for now i’ll accept bookmarking and adding your RSS feed to my
    Google account. I anticipate brand new updates and definately will share this website with my Facebook group.
    Talk soon!

  4. Reader says:

    I’d have to examine with you here. Which is not one thing I usually do! I take pleasure in reading a post that may make folks think. Additionally, thanks for permitting me to comment!

  5. LouraITrejos says:

    Hello there, You’ve done an incredible job.
    I’ll definitely digg it and personally suggest to my friends.
    I’m sure they’ll be benefited from this web site.

  6. Clarisa Brentley says:

    Oh my goodness! an amazing article dude. Thank you However I am experiencing issue with ur rss . Don’t know why Unable to subscribe to it. Is there anyone getting identical rss problem? Anyone who knows kindly respond. Thnkx

  7. Calvin says:

    I am very happy to read this. This is the type of manual that needs to be given and not the accidental misinformation that is at the other blogs. Appreciate your sharing this greatest doc.

Leave a Reply

Your email address will not be published. Required fields are marked *