How to Design a Basic Web Form with PHP

Visitor feedback is crucial for any website, whether you are building a corporate site or designing a site for your own personal use.  Adding a PHP-based form to your website can be quite easy, and only requires basic knowledge to write the HTML code and a PHP script to send the resulting complete form.  In order to run the script you’ll need to ensure your Apache serve is running at least PHP 4.1 (ideally, PHP 5), have a text editor to code the script (don’t use a Word processor since this can add extra symbols) and have clean IP address to ensure delivery of the emails.

Many PHP-based software platforms have integrated contact forms, such as WordPress, although you can add the following basic forms to nearly any PHP-based website on a platform independent basis.  Forms can be customized for contact information, lead generation and can be routed via email or directly through your customer relationships management (CRM) software.  Adding a form to your website can ensure your customers have a way of contacting your team, and the email can be routed to the proper member of your organization.

Writing an HTML Feedback Form

Adding an HTML (visual) form to your website requires creating HTML code to place within the body of your website. Keep in mind you can customize the form to match any particular requirements you might have with respect to the details they provide, including their name, contact information, point of origin, message and other details.

You can also set certain fields to be mandatory and others to be optional, as well as offering various selections via a radio or drop down field box.  This can be especially useful in cases where you need to qualify leads or contacts for business information, such as financial leads or call back requests. Writing a proper feedback form using a PHP script opens up options to help connect you with customers in a more meaningful way. There are virtually an unlimited number of options with PHP forms including inputs, multi-page options and a variety of methods to post the form.

In this example, we build a basic feedback form to capture a user’s name, email address and request:

<form method="post" action="form.php">
Name:<br />
Email: <input name="email" type="text" /><br />
Message:<br />
<textarea name="message" rows="20" cols="60">
</textarea><br />
<input type="submit" />
</form>

Incorporating the PHP Form Script

In the above form we used a form method of “post” which sends the data via PP script (form.php) in order to relay the message:

<?php
 $name = $_REQUEST['name'] ;
 $email = $_REQUEST['email'] ;
 $message = $_REQUEST['message'] ;
 mail( "[email protected]", "Contact Form from $name",
 $message, "From: $email" );
 header( "Location: http://www.yoursite.com/confirmation.html" );
?>

The form above is delivered to your email address with subject line Contact Form from {name}, which is pulled from the form submission, along with the message itself. When you set the header to confirmation.html you redirect the user to a thank-you page where you confirm the message has been sent. You can also utilize this page to track form submissions within your analytics.

Links

1 response so far ↓

avatar

1. Response by : coolguruji on Jun 9, 2011 at 2:55 am

thanks
your script is wonderful i have added this script in my website and its is working but i want to add some more feature in this script just like address information, website url, city, states, country, comments and more.
i think i can do it
actually my website is in search engine and now i am going to start advertising programme in my website that is why i need a contact form, from which advertiser can contact with me and order me.
thanks once again

Leave a Comment




 

Search Website

 
 
 

Top 10 PHP Hosting Providers

MyHosting - $4.00 USD
InMotion Hosting - $5.95 USD
WebHostingHub - $4.95 USD
JustHost - $3.95 CDN
iPage - $3.50 CDN
HostGator - $4.95 USD
FatCow - $3.67 USD
GreenGeeks - $4.95 USD
BlueHost - $6.95 USD
10 GoDaddy - $4.11 CDN