--> How To Create A Contact Form With PHP? | Experience Lab - Online business creation and development guide for bloggers and startups

How To Create A Contact Form With PHP?

Contact forms are one of the most integral parts of any website. They are what give it a humane touch. Almost every blog or website has one...




Contact forms are one of the most integral parts of any website. They are what give it a humane touch. Almost every blog or website has one. But you'd be surprised at how only a few people actually know how to make a contact form. Since they're so important, we figured it'd be a nice idea to teach beginners how to create them, instead of relying on any third-part to do the work for them. We previously shared a post on creating a responsive contact form using HTML and CSS. Today, we'll continue that tutorial, and teach you how to actually make a contact form work.


If you haven't gone through the first tutorial, then I'd strongly recommend you to do so.  That tutorial covered everything from markup to structure to styling a contact form. In this tutorial, we'll only cover the programming part, and we'll use PHP to make our contact actually work (accept inputs and send them to an email address). So if you know how to write the markup for a contact form, you're ready to move on!


Powering a contact form with PHP



Before we jump into the PHP, I'd like you to take a look at our basic structure first.




<form>

  <div id="form-inner">

    <label for="name">Name:</label>

    <input class="input" id="name" placeholder="Enter your name here" type="text" />

    <label for="email">Email:</label>

    <input class="input" id="email" placeholder="Enter a valid email address" type="email" />

    <label for="message">Message:</label>

    <textarea class="input textarea" id="message" placeholder="Type in your message here"></textarea>

    <input class="button" type="reset" value=" Reset" />

    <input class="button" type="submit" value="Submit" />

  </div>

</form>




First of all, we need to add an action to our form tag. So first, you need replace the top line that says "<form>", with;




<form method="post" action="index.php">




The method="post" attribute provides a way of extracting values from the input fields of a contact form using PHP. The action="index.php" attribute specifies the PHP file this data should be sent to. The PHP script for sending an email needs to be stored on a file. It can be a separate file, or the same HTML file that contains your form's markup. For this tutorial, we'll keep the script in the same file, so index.php should be the name of your file with the contact form.





Note: In order to add a PHP script to an HTML file, you need to change the file's extension from .html to .php. The HTML markup would work just fine even after the change. But keep in mind that you won't be able to run PHP files on your computer without having the PHP software installed on your computer. Typically, this software is only found on servers, but you can download it for free - it's a small software. You can either download it separately, or download a packaged software that contains other server software (recommended). Examples of such software include WAMP, XAMP, ZendServer, and so on. All are free to use, and readily available for download.





So now that we've set up the stage for our PHP script, it's time to actually start writing one.


Variables



First, we need to extract the data from the contact form, and store it in a variable (or container). Here's the script;




<?php

    $name = $_POST['name'];

    $email = $_POST['email'];

    $message = $_POST['message'];



    $from = 'From: MBT Contact';

    $to = 'contact@mybloggertricks.com';

    $subject = 'Contact Message';



    $body = "Sender name : $name\n Sender E-Mail : $email\n Message : $message";

?>




The script starts with the opening <?php tag, and ends with the closing ?> tag. The first three lines capture and store the values generated from the contact form. Note how we're using $_POST variables for data extraction. This is thanks to the method="post" attribute we used in our form tag.





The next three lines specify the source of the email generated, the destination email address, and the subject line. Notice the source says "MBT Contact". We can use the sender's name or email address here, but you probably want to know which message is coming to you via the contact form, and which is coming directly to your inbox.




The last line puts all the information received from the contact form into a single string for convenience.


Mail function


We're now ready to send our message. Take a look at the following script;




<?php

    if ($_POST['submit']) {

        if (mail ($to, $subject, $body, $from)) {

            echo '<p>Thank you for your feedback! We'll be in touch shortly</p>';

        } else {

            echo '<p>Something went wrong. Please try again!</p>';

        }

    }

?>



The first line (after the opening tag) contains a condition that checks whether the variable $_POST['submit'] has been created. This will only happen if a user clicks on the submit button. So in other words, the message won't be sent unless the user hits the Send button. Sounds reasonable?



Alright. Next, we have our mail () function inside another condition. This condition checks for the successful execution of the mail () function. If successful, a success message is displayed. Otherwise, the user is asked to try again.



Here's the final script;




<?php

    $name = $_POST['name'];

    $email = $_POST['email'];

    $message = $_POST['message'];



    $from = 'From: MBT Contact';

    $to = 'contact@mybloggertricks.com';

    $subject = 'Contact Message';

    $body = "Sender name : $name\n Sender E-Mail : $email\n Message : $message";



    if ($_POST['submit']) {

        if (mail ($to, $subject, $body, $from)) {

            echo '<p>Thank you for your feedback! We'll be in touch shortly</p>';

        } else {

            echo '<p>Something went wrong. Please try again!</p>';

        }

    }

?>



Did this tutorial help you? Leave us your feedback in the comments section below. Cheers :)

COMMENTS

Name

Affiliate Marketing,12,Announcement,34,Bing,9,Bitcoin,38,blog,7,Blogger Resources,42,Blogger Templates,4,blogger tricks,156,Blogging ethics,70,Blogging tips,198,Bugs and Errors,34,Business,9,Copyright Violation,9,CSS and HTMLTricks,95,Designs,8,drop down menu,7,eBook,12,Email Marketing,7,Events,30,Facebook,30,Facebook tricks,49,Google,157,Google AdSense,42,Google Analytics,7,Google Plus,51,Google Plus Tricks,38,Guest Posts,112,home,2,How To,77,Internet,1,JSON Feeds,25,Kitchen Recipes,2,Label Based Sitemap Themes,1,Make Money Online,108,Marketing,16,MBT Blogger Templates,7,Menus,1,News,146,Pages,1,Posts,10,presentations,15,Responsive,10,Reviews,7,SEO,307,Settings,6,Shortcode,15,Sitemap Themes,1,Social Media,155,Technology,7,Templates,1,Tips,2,Tools,1,Traffic Tips,80,Video,19,Web Designing,62,web hosting,18,Webmaster Tools,97,Widgets,199,wordpress,26,
ltr
item
Experience Lab - Online business creation and development guide for bloggers and startups: How To Create A Contact Form With PHP?
How To Create A Contact Form With PHP?
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2SYfPEhLo7EE3k7Bg-TRKrRT-tsalgGeGy_HFInLpD_tIK-pfCVlw2aSy3en20Z5_6NL-y8Z6HKBxfjCJw2hrtqlfnZE4JwQkWrLp-pmbGRWxOwW40_GXvgKkAD4hsS4y-ja8s1iAQliS/s1600/contact-form.jpg
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2SYfPEhLo7EE3k7Bg-TRKrRT-tsalgGeGy_HFInLpD_tIK-pfCVlw2aSy3en20Z5_6NL-y8Z6HKBxfjCJw2hrtqlfnZE4JwQkWrLp-pmbGRWxOwW40_GXvgKkAD4hsS4y-ja8s1iAQliS/s72-c/contact-form.jpg
Experience Lab - Online business creation and development guide for bloggers and startups
https://www.experiencelab.info/2013/12/how-to-create-contact-form-with-php.html
https://www.experiencelab.info/
https://www.experiencelab.info/
https://www.experiencelab.info/2013/12/how-to-create-contact-form-with-php.html
true
2959477579779989044
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share. STEP 2: Click the link you shared to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy