simple contactus page-php-code-demo



 Simple contactus page-php-code-demo 

 with javascript validation


<?php
 session_start();
 include "include/connection.php";
 include "include/functions.php";
 include "include/header.php";
//$r=getCMSPageContent(8);
 $current_date=date('Y-m-d');
 $msg='';

 if(isset($_POST['submit_contact']))
 {
  $fname=trim($_POST['fname']);
  $lname=trim($_POST['lname']);
  $email=trim($_POST['email']);
  $contact_no=trim($_POST['contact_no']);
  $address=addslashes(trim($_POST['address']));
  $ip_add=$_SERVER["REMOTE_ADDR"];
 
  $sql="insert into `sp_contactus` set `fname`='$fname',
                                          `lname`='$lname',
                                          `email`='$email',
                                          `contact_no`='$contact_no',
                                          `address`='$address',
                                          `ip_add`='$ip_add',
                                          `creation_date`='$current_date'
                                          ";
   
    $res=mysql_query($sql);
    if($res)
    {
      $msg="Your contact details are recive ..";   
    }
 }

 ?>

<div class="container">
<div class="row" style="margin-top:30px;">
<div class="span6">
<div class="thumbnail" style="padding:30px;">
      <h4>We are Located at :-</h4>
      <p style="text-align:justify;">
          <address>
    <strong>TestTeam</strong><br>
            PO Box 654, Westmead,<br>
            NSW, 26545<br>
            Australia<br>
           
    </address>
    
    <address>
    <strong>Email us at</strong><br>
    <a href="mailto:#">info@test.com</a>
    </address>
      </p>
</div>
</div>
<div class="span6">
<p>

    <form class="form-horizontal" name="contact" id="contact" action="" method="post">
    <legend>Contact Us</legend>
    <div class="control-group">
    <label class="control-label" for="inputEmail">First Name</label>
    <div class="controls">
    <input type="text" name="fname" id="fname"  placeholder="First Name">
    </div>
    </div>
    <div class="control-group">
    <label class="control-label" for="inputEmail">Last Name</label>
    <div class="controls">
    <input type="text" name="lname" id="lname" placeholder="Last Name">
    </div>
    </div>
    <div class="control-group">
    <label class="control-label" for="inputPassword">Email Id</label>
    <div class="controls">
    <input type="email" name="email" id="email" placeholder="Email Id">
    </div>
    </div>
    <div class="control-group">
    <label class="control-label" for="inputPassword">Contact No</label>
    <div class="controls">
    <input type="text" name="contact_no" id="contact_no" placeholder="Contact No">
    </div>
    </div>
    <div class="control-group">
    <label class="control-label" for="inputPassword">Address</label>
    <div class="controls">
    <textarea name="address" id="address" rows="5"></textarea>
    </div>
    </div>
    <div class="control-group">
    <div class="controls">
    <input type="hidden" name="submit_contact" value="submit_contact" >
    <button type="button" class="btn btn-primary" onClick="return submit_contact_us()">Send</button>
    </div>
    </div>
    </form>
</p>
    </div>
</div>
<?php include("include/footer.php");?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

<script language="javascript">
function submit_contact_us()
{
 var fname=$("#fname").val();
 var lname=$("#lname").val();
 var email=$("#email").val();
 var contact_no=$("#contact_no").val();
 var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
 
 if(fname=='')
 {
   alert('Please enter first name');   
   $("#fname").focus();
   return false;
 }
 else if(lname=='')
 {
   alert('Please enter last name');   
   $("#lname").focus();
   return false;
 }
 else if(email=='')
 {
   alert('Please enter your email');   
   $("#email").focus();
   return false;
 }
 else if(!filter.test(email))
 {
   alert('Please provide a valid email address');   
   $("#email").focus();
   return false;   
 }
 else if(contact_no=='')
 {
   alert('Please enter your contact no');   
   $("#contact_no").focus();
   return false;
 }
 else if(isNaN(contact_no))
 {
   alert('Contact no should be numeric');   
   $("#contact_no").focus();
   return false;
 }else {
  document.contact.submit();
 }
}


<?php if($msg!=''){ ?>
 alert("<?php echo $msg; ?>");   
<?php } ?>

</script>