How To Create PHP Contact us Form for Website

PHP Contact form

In this post we will learn How To Create PHP Contact Form for website and How To Add PHP Contact Form To Our Website.

we are know contact form of websites led user to communicate with website owners. This shows a genuine or loyal behavior of an organization towards its customers

the contact form needs to have two parts: the client side ‘front end’ of the form and the server side ‘back end’ of the form. The client side of the form is coded in HTML plus some style CSS and JavaScript.

We will first create a simple contact form with 4 fields – Email address, Name, Website, Massage. I will use a table to align the 4 fields and the Send button. Create a new file and paste the code below in it. Save it as mail.php and upload it to your web server in public Folder. Now, you have a web page http://www.yourwebsite.com/mail.php with a simple contact form on it.

this is front side basic contact form you can make this contact form more stylish using css

Now see what’s been added Take a look at the opening <form>

tag. We’ve added in this form two new attributes; method and action.

The method=”post” attribute provides the PHP script with a method of extracting the values from the form, so the script can then do what it wants with those values. The action=”mail.php” attribute simply tells us the location of the PHP script; in this case, because it’s only a few lines of code, we’re going to insert it just above the form in our mail.php file. So when the form is submitted, it will go to mail.php and find the script.

Take another look at the HTML above and you will see that each of the form elements have the name attribute. The PHP uses this attribute to extract the data from each of the form elements. Let’s look at how it does this

If you’ve never used PHP before, it might look a bit confusing – but it’s actually really simple. The $_POST part is a built in PHP function which is used to collect data from a form that uses method=”post” like ours does. The part that immediately follows this function, [‘name’] must correspond to the name attribute from the form input; in the first instance, name=”name”!

So, this will collect whatever data has been entered into the name field – now we need something to store that data in. This is where variables come into play. In this case, $name is the variable that we will use to store the data from the name field. We can then do the same for the email and message fields. Simple!

now save the your php file and give the name mail.php and host it in public folder you can try free web hosting service for test your php contact form

One Comment on “How To Create PHP Contact us Form for Website”

  1. Hello my loved one! I want to say that this article is awesome, nice written and come with approximately all important infos. I would like to see extra posts like this .

Leave a Reply

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