How to make basic contact form in PHP

How to make basic contact form in PHP

Here we will explore the method through which a basic form could get designed. None of the websites considered as complete without contact form because it is the main interface through an end user interacts. Hence knowing the basic steps in order to create a contact form is worthy. Below we are presenting the concept.

At the very initial stage, you must choose a location from where the form will get reflected. Mostly, the forms start the tags of form in HTML as mentioned:

< form > & < /form >

Hence the created form will execute when the submit button is pressed and it will show the content which will be deigned during the content. We shall mention the same in the upcoming points. Once it is done, then the next step will be creation of mail.php file which will be created using POST method. The ‘POST’ method will initiate the PHP scrip to send the email when any user fills the form and submit the same. It connects the user and service provider so that service provider could receive their queries/complaints/suggestions at their email box.

Action and Method of mail.php

< form action="mail.php" method="POST" >

Now the one thing left is the understanding of data type which will get inserted by the end user. Like ‘textarea’ which will allow them enough space to type their concerns. Each and every text area will be given a specific name through the PHP document so that aggregation of the information could be easier.

Now we are exploring the code which needs to be used in the format of HTML in order to create a simple contact form. Let’s understand through an example:

HTML Form Code

< form action="mail.php" method="POST" >

< p >Name< /p > < input type="text" name="name" >

< p >Email< /p > < input type="text" name="email" >

< p >Message< /p > < textarea name="message" rows="6" cols="25" >

< input type="submit" value="Send" >< input type="reset" value="Clear" >

< /form >

In order to create a form, the above mentioned code can be used and if needed, modifications could be done based on the need.

Addition of dropdown box in contact form

In forms we need to add dropdown box as well hence the below mentioned HTML code will help in creation

< form action="mail.php" method="POST" >

< p >Name< /p > < input type="text" name="name" >

< p >Email< /p > < input type="text" name="email" >

< p >Phone< /p > < input type="text" name="phone" >

< p >Dropdown Box< /p >

< select name="dropdown" size="1" >

< option value="Option1" >Option1< /option >

< option value="Option2" >Option2< /option >

< option value="Option3" >Option3< /option >

< option value="Option4" >Option4< /option >

< /select >

< br / >

< p>Message< /p >< textarea name="message" rows="6" cols="25">< /textarea >

< input type="submit" value="Send" >< input type="reset" value="Clear" >

< /form >

Addition of Radio Button in Contact form

If you are keen to add radio button in the contact form then simply copy the code and insert in the main code.

< p >Request Phone Call:< /p >

Yes:< input type="checkbox" value="Yes" name="call" >< br / >

No:< input type="checkbox" value="No" name="call" >< br / >

Complete code (HTML) for creation of contact form:

< form action="mail.php" method="POST" >

< p>Name< /p > < input type="text" name="name" >

< p>Email< /p > < input type="text" name="email" >

< p>Phone< /p > < input type="text" name="phone" >

< p >Request Phone Call:< /p >

Yes:< input type="checkbox" value="Yes" name="call" >< br / >

No:< input type="checkbox" value="No" name="call" >< br / >

< p >Website< /p > < input type="text" name="website" >

< p >Priority< /p >

< select name="priority" size="1" >

< option value="Low" >Low< /option >

< option value="Normal" >Normal< /option >

< option value="High" >High< /option >

< option value="Emergency" >Emergency< /option >

< /select >

< br / >

< p >Type< /p >

< select name="type" size="1" >

< option value="update" >Website Update< /option >

< option value="change" >Information Change< /option >

< option value="addition" >Information Addition< /option >

< option value="new" >New Products< /option >

< /select >

< br / >

< p >Message< /p >< textarea name="message" rows="6" cols="25" >< /textarea >< br / >

< input type="submit" value="Send" >< input type="reset" value="Clear" >

< /form >

Corresponding to the mentioned HTML, below mentioned PHP code will be needed:

< ?php

$name = $_POST['name'];

$email = $_POST['email'];

$phone = $_POST['phone'];

$call = $_POST['call'];

$website = $_POST['website'];

$priority = $_POST['priority'];

$type = $_POST['type'];

$message = $_POST['message'];

$formcontent=" From: $name \n Phone: $phone \n Call Back: $call \n Website:

$website \n Priority: $priority \n Type: $type \n Message: $message";

$recipient = "youremail@here.com";

$subject = "Contact Form";

$mailheader = "From: $email \r\n";

mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");

echo "Thank You!";

? >


Top
Quick Contact