Simple php contact form to email script

Contact forms are the easiest way to let the visitor contact you, the visitor can inform you without revealing your email address on the web page and you can know what people think about your web site,i have used this php code it's 100% working and simple so you can add more text fields change subject text or add your own subject text.   


use table to align  form contents and style it adding some css, copy and past this code to your contact form and save it as contact.html

 <form class="email" action="mail.php" method="post">  
 <p>Name:</p>  
 <input type="text" name="name" />  
 <p>E-mail:</p>  
 <input type="text" name="email" />  
 <p>Subject:</p>  
 <input type="text" name="subject" />  
 <p>Message:</p>  
 <textarea name="message"></textarea></p>  
 <input class="send" type="submit" value="Send">  
 </form>  


Add your email address to "YOUR EMAIL ADDRESS"  then save it  as mail.php

 <?php  
 /* Set e-mail recipient */  
 $myemail = "YOUR EMAIL ADDRESS";  
 /* Check all form inputs using check_input function */  
 $name = check_input($_POST['name'], "Enter your name");  
 $subject = check_input($_POST['subject'], "Enter a subject");  
 $email = check_input($_POST['email']);  
 $message = check_input($_POST['message'], "Write your message");  
 /* If e-mail is not valid show error message */  
 if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))  
 {  
 show_error("E-mail address not valid");  
 }  
 /* Let's prepare the message for the e-mail */  
 $message = "  
 Name: $name  
 E-mail: $email  
 Subject: $subject  
 Message:  
 $message  
 ";  
 /* Send the message using mail() function */  
 mail($myemail, $subject, $message);  
 /* Redirect visitor to the thank you page */  
 header('Location: thanks.html');  
 exit();  
 /* Functions we used */  
 function check_input($data, $problem='')  
 {  
 $data = trim($data);  
 $data = stripslashes($data);  
 $data = htmlspecialchars($data);  
 if ($problem && strlen($data) == 0)  
 {  
 show_error($problem);  
 }  
 return $data;  
 }  
 function show_error($myError)  
 {  
 ?>  
 <html>  
 <body>  
 <p>Please correct the following error:</p>  
 <strong><?php echo $myError; ?></strong>  
 <p>Hit the back button and try again</p>  
 </body>  
 </html>  
 <?php  
 exit();  
 }  
 ?>  

Create a page saying the email was sent successfully
Add your message "email sent successfully"  then save it  as thanks.html

 <html>  
 <body>  
 <p>email sent successfully</p>  
 </body>  
 </html>  
Simple php contact form to email script Simple php contact form to email  script Reviewed by Unknown on 12:14 AM Rating: 5
Powered by Blogger.