Rss Feed
  1. Simple mail sending in PHP, AJAX

    Saturday, 13 October 2012

     HTML Form

    <form action="sendmail.php" method="post">
    <div style="margin:10px 0;"> Name.</div>
     <input name="username" id="username" type="text" style="width:300px; height:35px;" />         
    <span id="userspan"></span>

    <div style="margin:10px 0;">Contact No.</div>
    <input name="contact" id="contact" type="text" style="width:300px; height:35px;" />
    <span id="contactspan"></span>

    <div style="margin:10px 0;">Company Name</div>
    <input name="company" id="company" type="text" style="width:300px; height:35px;" />
    <span id="companyspan"></span>

    <div style="margin:10px 0;">Email</div>
    <input name="email" id="email" type="text" style="width:300px; height:35px;" />
    <span id="emainspan"></span>

    <div style="float:left; margin:15px 0 0 110px; padding:0;"><a onclick="return sendcontact()"><img src="img/submitbutton.png" width="82" height="28" alt="submit" /></a></div>
    </div>
    </form>



    Javascript Ajax Call

    <script type="text/javascript">

      function sendcontact()
      {
          document.getElementById('userspan').innerHTML = '';
          document.getElementById('contactspan').innerHTML = '';
          document.getElementById('companyspan').innerHTML = '';
          document.getElementById('emailspan').innerHTML = '';
         
          var name = document.getElementById("name").value;
          var comp = document.getElementById("company").value;
          var con = document.getElementById("contact").value;
          var email = document.getElementById("email").value;

          if((name == "" || name == null))
          {
              document.getElementById('name').focus();
              document.getElementById('userspan').innerHTML = 'Please Provide your Name';
              return false;
          }
         
          else if((con == "" || con == null))
          {
              document.getElementById('contact').focus();
              document.getElementById('contactspan').innerHTML = 'Please Provide your Contact No';
              return false;
          }
         
          else if((comp == "" || comp == null))
          {
              document.getElementById('company').focus();
              document.getElementById('companyspan').innerHTML = 'Please Provide your Company name';
              return false;
          }


          else if((email == "" || email == null))
          {
              document.getElementById('email').focus();
              document.getElementById('emailspan').innerHTML = 'Please Provide your Company name';
              return false;
          }
       
          if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
              xmlhttp=new XMLHttpRequest();
          }
          else
          {
              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
         
          xmlhttp.onreadystatechange=function()
          {
              if (xmlhttp.readyState==4 && xmlhttp.status==200)
              {
                  document.getElementById("contactres").innerHTML=xmlhttp.responseText;
              }
          }
         
          var str = name + '&con=' + con + '&comp=' + comp + '&email=' + email;
          xmlhttp.open("GET","sendmail.php?name="+str,true);
          xmlhttp.send();
      }
      </script>




    PHP Script

    <?php

    $name = $_GET['name'];
    $contact = $_GET['con'];
    $company = $_GET['comp'];
    $email = $_GET['email'];

    $to = 'example1@example.com, example2@example.com';
    $message = "Hi, You have got a contact from Your Web" . "\n" . "\n";
    $message .= "Name : " . $name . "\n" . "Company : " . $company . "\n" . "Contact Number : " . $contact . "\n";
    $subject = "Mail from your website";
    $from = $email;
    $headers .=  "From: ". $from;

    if(mail($to,$subject,$message,$headers))
    {
        echo "Thank you for your interest. We will get in touch with you shortly.";
    }
    else
    {
        echo "Sorry, there exist some problem. Please try again later.";
    }
    ?>

  2. 0 comments:

    Post a Comment