PDA

View Full Version : Contact form for my website


stu_c
02-24-2015, 09:52 AM
Hi all
I am trying to have a basic contact form for my website with the following fields

Name*
Phone Number
Email Address*
Message*

and then Email to my Email address once submitted can anyone help me sort one out please?

I have been trying now for days and I am to the stage of giving up :(

edbr
02-25-2015, 01:49 AM
heres one

form<?php include 'validation.php';?>
<!DOCTYPE HTML>
<html>
<head>
<title>PHP Contact Form with Validation</title>
<style>
span{
color:red;
}

div.container{
width: 960px;
height: 610px;
margin:35px auto;

}

div.main{
width: 350px;
padding: 10px 50px 30px;
border: 2px solid gray;
border-radius: 10px;
font-family: raleway;
float:left;
}

input[type=text],textarea {
width: 97.7%;
height: 34px;
padding-left: 5px;
margin-bottom: 5px;
margin-top: 5px;
border: 2px solid #ccc;
color: #4f4f4f;
font-size: 16px;
border-radius: 5px;
}

textarea{
resize:none;
height:80px;
}

label{
color: #464646;
text-shadow: 0 1px 0 #fff;
font-size: 14px;
font-weight: bold;
}

.submit{
padding: 10px;
text-align: center;
font-size: 18px;
background: linear-gradient(#ffbc00 5%, #ffdd7f 100%);
border: 2px solid #e5a900;
color: #ffffff;
font-weight: bold;
cursor: pointer;
text-shadow: 0px 1px 0px #13506D;
width: 100%;
border-radius: 5px;
}

</style>
</head>
<body>
<div class="container">
<div class="main">
<h2>PHP Contact Form with Validation</h2><hr/><br>
<form method="post" action="contact_form.php">
<label>Name :</label><br>
<input class="input" type="text" name="name" value="">
<span class="error"><?php echo $nameError;?></span><br><br>
<label>Email :</label><br>
<input class="input" type="text" name="email" value="">
<span class="error"><?php echo $emailError;?></span><br><br>
<label>Telephone :</label><br>
<input class="input" type="text" name="tel" value="">
<span class="error"><?php echo $telError;?></span><br><br>
<label>Message :</label><br>
<textarea name="message" value=""></textarea>
<span class="error"><?php echo $messageError;?></span><br><br>
<input class="submit" type="submit" name="submit" value="Submit">
<span class="success"><?php echo $successMessage;?></span>
</form>
</div>

</div>
</body>
</html>
<!--html ends here-->


validation and mail sender validation.php
<?php
$to="admin@home.com";
// Initialize variables to null.
$name =""; //Sender Name
$email =""; //Sender's email ID
$tel =""; //Subject of mail
$message =""; //Sender's Message

$nameError ="";
$emailError ="";
$telError ="";
$messageError ="";
$successMessage ="";

//On submitting form below function will execute

if(isset($_POST['submit']))
{
// checking null values in message
if (empty($_POST["name"])){
$nameError = "Name is required";
}
else {
$name = test_input($_POST["name"]);
// check name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)){
$nameError = "Only letters and white space allowed";
}
}
// checking null values in message
if (empty($_POST["email"])) {
$emailError = "Email is required";
}
else {
$email = test_input($_POST["email"]);
}
// checking null values in message
if (empty($_POST["tel"])) {
$telError = "telephone is required";
}
else {
$tel = test_input($_POST["tel"]);
if (!preg_match("/^[1-9][0-9]*$/",$tel )){
$telError = "Only Numbers allowed";
}


}
// checking null values in message
if (empty($_POST["message"])) {
$messageError = "Message is required";
}
else {
$message = test_input($_POST["message"]);
}
// checking null values in all fields
if( !($name=='') && !($email=='') && !($tel=='') &&!($message=='') )

{// checking valid email
if (preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email) &&(preg_match("/^[1-9][0-9]*$/",$tel ))) {

$header= $name."<". $to .">";
$headers = "mydomain.com";
/* Let's prepare the message for the e-mail */
$msg = "

Name: $name
E-mail: $email
Telephone: $tel
Message: $message

";



/* Send the message using mail() function */
if(mail($to, $headers, $msg ) )
{
$successMessage = "Message sent successfully.......";
header('Location: thank_you.html/');
exit;
}
}
else { $emailError = "Invalid Email"; }

}
}
// function for filtering input values
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
edit the $to at the top
edit location header on line 80.
i cobbled this quickly but it should do your purpose