How to Create a PHP Contact Form

I have heard many of my friends searching about ” How to make a PHP contact form ” and not getting useful search results or getting the right thing but not in a appropriate manner. So to overcome this problem I am sharing an easy following procedure in a simple manner to create a simple but nicely working php contact form easily.

Check it out – How to Download Instagram Videos Using Telegram

After you follow the steps below the outcome php contact form would be like this-

php-contact-form

So lets start with a simple html form containing three fields for Name, Email and Message. Where Name and Email would be of input type “text” and Message field of  “textarea”. Check out the code below.

 

contact-form.php (with Bootstrap)


<div class="row">

<div class="col-lg-12">

<h5 class="text-sm-center">We would like to hear more from you</h5>

</div>

</div>


<div class="row">

<div class="col-lg-8 offset-lg-2 text-xs-center">
<form id="contact" action="/contact-request.php" method="post" name="contactus">

<div class="row control-group">

<div class="col-xs-2"><label for="contact_name">Name</label></div>


<div class="form-group col-xs-10 floating-label-form-group controls"><input id="contact_name" class="form-control" name="contactname" required="" type="text" placeholder="Full Name" data-validation-required-message="Please enter your first name." /></div>

</div>


<div class="row control-group">

<div class="col-xs-2"><label for="contact_email">Email</label></div>


<div class="form-group col-xs-10 floating-label-form-group controls"><input id="contact_email" class="form-control" name="contactemail" required="" type="email" placeholder="Email Address" data-validation-required-message="Please enter your email address." /></div>

</div>


<div class="row control-group">

<div class="col-xs-3"><label for="contact_message">Message</label></div>


<div class="form-group col-xs-9 floating-label-form-group controls"><textarea id="contact_message" style="width: 100%; height: 135px;" name="contactmessage"></textarea></div>

</div>


<div class="row">

<div class="form-group col-lg-4 offset-lg-4 floating-label-form-group controls text-xs-center"><button id="contact_submitbtn" class="btn btn-default font-white btn-back login_btn" name="conatctsubmit" type="submit">Submit</button></div>

</div>

</form>
</div>

</div>

These classes with div tags in the code above is associated with Bootstrap. If you don’t know about Bootstrap then either you can search it on Google or you can just make a simple for using html tags and style it according to your need using CSS.

 

contact-form.php (without Bootstrap)


<h5>We would like to hear more from you</h5>


<form id="contact-form" action="/contact-request.php" method="post" name="contact-form"><label for="contact_name"><label for="contact_name">Name <input id="contact_name" name="contactname" type="text" placeholder="Full Name" /></label></label><label for="contact_email">Email</label>
<input id="contact_email" name="contactemail" type="email" placeholder="Email Address" /><label for="contact_message">Message</label>
<textarea id="contact_message" style="width: 100%; height: 135px;" name="contactmessage"></textarea><input id="contact_submitbtn" name="conatctsubmit" type="submit" /></form>

Now the php contact form structure is created. But it will not work as we haven’t created the php script yet. So lets dive into the fun part, php.

Create another php file and name it is contact-request.php as shown below:

contact-request.php

<?php

$name = $_POST['contactname'];
$email = $_POST['contactemail'];
$msg = $_POST['contactmessage'];

$to = 'youremail@example.com'; // Change youremail@example.com with your personal email addrress
$subject = 'Contact Us Feedback'; // Give the email a subject
$message = $name;
$message .= $msg;
mail($to, $subject, $message, $email); // Send email

header('Location: /contact-us.php');
?>

And thats it. Your simple php contact form is ready. Any time a user fill the form and clicks on submit button, you will get an email (don’t forget to replace youremail@example.com with your personal email) containing all the information filled in the form.

Please share your problems, feedback and views in the comments. Stay tuned for more tutorials.

Leave a Reply

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