PHP Contact Form

PHP Contact FormFirst, let's create the html section. I made a simple form, but it gets the job done. You can customize it if you want, but remember to use the same form variables.

<form action="<?=$file_location;?>" method="post">
<input type="hidden" name="hidden" value="true">
<table>
<tr><td>From:</td><td><input type="text" name="from" value="" size="40"></td></tr>
<tr><td>Subject:</td><td><input type="text" name="subject" value="" size="40"></td></tr>
<tr><td>Message:</td><td><textarea name="message" cols="60" rows="10"></textarea></td></tr>
<tr><td colspan="2" align="right"><input type="submit" style="font-weight: bold" value="Send"></td></tr>
</table>
</form>

Now, for the PHP code, which should be placed above the HTML section.

This will be the first part of your code, which will, in term, determine if the form is being processed.

The isset function checks to see if the variable exist, this function has various alternatives.

<?php if(isset($hidden)){

After, if the form is indeed processed, check for all the variables using the isset function. You can alter the message in the echo, if you feel like it. =)

if(!isset($from) or !isset($subject) or !isset($message)) {
echo("Your message couldn't be sent, because one or more fields weren't complete.");
}

We can finally use the mail function. Change the $to variable to your email address. Once again, you can edit the message in the echo!

else {
$to = "Your@email.com";
$from = "$from";
$subject = "$subject";
$message = "$message";
mail($to, $subject, $message,"From: $from");
echo("Your message was sent, I'll reply soon.");
}
}
?>

Final:

<?php
if(isset($hidden)){
if(!isset($from) or !isset($subject) or !isset($message)) {
echo("Your message couldn't be sent, because one or more fields weren't complete.");
} else {
$to = "Metalish@gmail.com";
$from = "$from";
$subject = "$subject";
$message = "$message";
mail($to, $subject, $message,"From: $from");
echo("Your message was sent, I'll reply soon.");
}
}
?>

<form action="<?=$file_location;?>" method="post">
<input type="hidden" name="hidden" value="true">
<table>
<tr><td>From:</td><td><input type="text" name="from" value="" size="40"></td></tr>
<tr><td>Subject:</td><td><input type="text" name="subject" value="" size="40"></td></tr>
<tr><td>Message:</td><td><textarea name="message" cols="60" rows="10"></textarea></td></tr>
<tr><td colspan="2" align="right"><input type="submit" style="font-weight: bold" value="Send"></td></tr>
</table>
</form>

Enjoy !


Related Articles

  • How to make a simple form mailer with PHP
    As you may be well aware, displaying your e-mail address on your website can lead to e-mail harvesters picking up your address and adding it to hundreds or even thousands of lists used by spammers...
  • Form Handling
    We are going to be working with the $_Post or $_Get arrays. They basically tell the form if i needs to post something, or get something. In this case we are going to be showing you the basics of a contact email form, so we will be using the $_Post array.
  • PHP Contact Form
    First, lets create the html section. I made a simple form, but it gets the job done. You can customize it if you want, but remember to use the same form variables.
  • The Importance of Having a Feedback Form in Your E-commerce Website
    Internet marketing is a different ball game altogether when compared to traditional marketing. A brick and mortar store employs salespersons to guide and advise its customers. No such equivalent of a salesperson exists for an online store. This could be one of the reasons why people shy away from on...
  • Contact Form
    Here I will explain to you how you can create a simple contact form. The form will collect data from the visitor and send it to any email address you please. The only requirement for this tutorial is for you to have .php enabled on your server. If you dont want to read through the whole tutorial a....
  • Form Email
    Learn to send mail with the php mail(); function.
  • E-mail form using HTML and PHP
    Sometimes its a nice touch to include an e-mail feedback form on your contact page or elsewhere on your web site. Lets create an e-mail form using html and a simple php script to send it along.
  • How to Implement the CAPTCHA with PHP and GD
    This article is a gas, it will show you How to Implement the CAPTCHA with PHP and GD. All the necessary code is laid out nice and neat by the author who has also provided you with helpful graphics to make it easy to understand.
  • Good 24/7 Hosting Customer Support -- Is It Possible?
    Even if you are an experienced webmaster you will need to contact the support team time from time in order to solve problems which can be either on your side or web hosting company side...
  • Domain Names - Free Privacy - How to Protect your Privacy When You Register a Domain Name
    Are you worried about your privacy online when starting a website? You should be. When you register domain names - free privacy settings from your registrar can protect you from spam, prying eyes, or worse.

Contact Web Design Outsource and get started today

Need Website Designing, Development, Redesigning, Maintenance and SEO services or help growing your company's web presence? Request a free Quote Now.