Pandu

Pandu

  • NA
  • 18
  • 19.6k

contact form for a website.

Feb 25 2014 3:54 PM
Hello guys,
I am creating a website. I want to use a simple contact form in it.To mail to contact the owner of the site.  I got my form and validations on my form using HTML, Java Script and PHP. My form gives the message that email is sent. But I could not get anything in my mail box  of (yahoo or gmail) do I need to configure my mail box also for this and how to do that? I am working on localhost using Xampp. Thank you for your time and help.
My code is in two files as follows. form.php and contact.php
form.php
<html>
<head>
<title>Try the simple form with validation using java script</title>
<script language="JavaScript">
function Validator()
{
var x=document.forms["myForm"]["fname"].value;
if (x==null || x=="")
  {
  alert("First name must be filled out");
  return false;
  }
var y=document.forms["myForm"]["email"].value;
if (y==null || y=="")
  {
  alert("Email must be filled out");
  return false;
  }
  var z=document.forms["myForm"]["email"].value;
var atpos=z.indexOf("@");
var dotpos=z.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=z.length)
  {
  alert("Not a valid e-mail address");
  return false;
  }
}
</script>
 
</head>
<body>
<form name="myForm" action="contact.php" method="POST"  onsubmit="return Validator()">
    <input type="hidden" name="action" value="submit"/> Your name:<br />
    <input name="fname" type="text" value="" size="30"/> <br /> Your email:<br />
    <input name="email" type="text" value="" size="30"/> <br /> Your message:<br />
    <textarea name="message" rows="7" cols="30"> </textarea> <br />
    <input type="submit" value="Send email"/>
</form> 
</body>
</html>


and contact.php as
 <?php 
 $to = "[email protected]"; 
 $subject = "Contact Us"; 
 $email = $_REQUEST['email'] ; 
 $message = $_REQUEST['message'] ; 
 $headers = "From: $email"; 
 $sent = mail($to, $subject, $message, $headers) ; 
 if($sent) 
 {print "Your mail was sent successfully"; }
 else 
 {print "We encountered an error sending your mail"; }
 ?>