Friday, February 1, 2013

Sending form details to admin via e-mail on clicking Submit button

We need a mail server for this purpose. For example : for sending mails to gmail, we have smtp server. There are various other softwares that are available on internet. If you have all the requirements then you can easily perform this task with the following code :

HTML code for this :

<html>
<body>
    <form action="mail.php" method="post">
FIRST NAME : <input type="text" name="firstname"><br></br>
LAST NAME : <input type="text" name="lastname"><br></br>
Date Of Birth : <input type="text" name="dob"><br></br>
GENDER : <input type="radio" name="gender" value="male">Male
             <input type="radio" name="gender" value="female">Female<br></br>
EMAIL : <input type="text" name="email"><br></br>
CITY : <input type="text" name="city"><br></br>
               <center><input type="submit"></font></center>
</form>
</body>
</html>

PHP code i.e. "mail.php" file : 

<?php
$to="example@gmail.com";
$subject="Registration";
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$dob=$_POST['dob'];
$gender=$_POST['gender'];
$email=$_POST['email'];
$city=$_POST['city'];
$msg_body="First Name : $firstname\n";
$msg_body.="Last Name : $lastname\n";
$msg_body.="Date of Birth : $dob\n";
$msg_body.="Gender : $gender\n";
$msg_body.="E-Mail : $email\n";
$msg_body.="City : $city\n";
mail( $to, $subject,$msg_body);
header("Location: thankyou.html");
?>