...">

Php send mail and show success message on the same page

I have a small form in html:

 <form id="contact" action="contact.php" method="post"> 
  <table class="contact" width="400" border="0" cellspacing="2" cellpadding="0"> 
    <tr>
      <td >Your name:</td> 
      <td ><input name="name" type="text" id="name" size="32"></td> 
    </tr>
    <tr> 
      <td>Email address:</td> 
      <td><input name="email" type="text" id="email" size="32"></td> 
    </tr>
    <tr> 
      <td>Comment:</td> 
      <td>
        <textarea name="comment" cols="45" rows="6" id="comment" ></textarea>
      </td> 
    </tr>
    <tr>
      <td colspan="2" style="text-align:center;">
        <input type="submit"  value="Submit" style="padding: 3px 22px;" />
      </td>
    </tr>
  </table> 
</form>

In the contact.php file, I have this code:

    if (isset($_POST['submit'])) 
    { 
      $name = $_POST['name'];
      $email = $_POST['email'];
      $ToEmail = 'test@test.com';
      $EmailSubject = 'Site contact form '; 
      $mailheader = "From: ".$_POST["email"]."\r\n"; 
      $mailheader .= "Reply-To: ".$_POST["email"]."\r\n"; 
      $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
      $MESSAGE_BODY = "Name: ".$_POST["name"]."<br>"; 
      $MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>"; 
      $MESSAGE_BODY .= "Subject:".$_POST['subject']."<br />";  
      $MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"])."<br>"; 
      mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
     }
  ?>

Now it works fine. But when the user clicks the submit button, he simply redirects to the contact.php file. I want that when the user clicks on the button, he will display a success message in the same html file as a lightbox, and the message will also be sent. So can someone tell me how to do this? Any help and suggestions will be really noticeable. Thanks

Update

. "contact.html", - "contact.php", "success" "failure" "contact.html" "contact.php".

+4
2

- contact.php

<?php
if (isset($_POST['submit'])) { 
      $name = $_POST['name'];
      $email = $_POST['email'];
      $ToEmail = 'test@test.com';
      $EmailSubject = 'Site contact form '; 
      $mailheader = "From: ".$_POST["email"]."\r\n"; 
      $mailheader .= "Reply-To: ".$_POST["email"]."\r\n"; 
      $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
      $MESSAGE_BODY = "Name: ".$_POST["name"]."<br>"; 
      $MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>"; 
      $MESSAGE_BODY .= "Subject:".$_POST['subject']."<br />";  
      $MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"])."<br>"; 
      if(mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader))
      {
      echo "<script>alert('Mail was sent !');</script>";
      echo "<script>document.location.href='contact.php'</script>";
      }
      else
      {
      echo "<script>alert('Mail was not sent. Please try again later');</script>";
      }
     }
+3

<html>
<head>
</head>
<body>
 <form id="contact" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> 
  <table class="contact" width="400" border="0" cellspacing="2" cellpadding="0"> 
    <tr>
      <td >Your name:</td> 
      <td ><input name="name" type="text" id="name" size="32"></td> 
    </tr>
    <tr> 
      <td>Email address:</td> 
      <td><input name="email" type="text" id="email" size="32"></td> 
    </tr>
    <tr> 
      <td>Comment:</td> 
      <td>
        <textarea name="comment" cols="45" rows="6" id="comment" ></textarea>
      </td> 
    </tr>
    <tr>
      <td colspan="2" style="text-align:center;">
        <input type="submit"  value="Submit" style="padding: 3px 22px;" />
      </td>
    </tr>
  </table> 
</form>
<?php
   if(isset($_POST))
   {
       $name = $_POST['name'];
      $email = $_POST['email'];
      $ToEmail = 'test@test.com';
      $EmailSubject = 'Site contact form '; 
      $mailheader = "From: ".$_POST["email"]."\r\n"; 
      $mailheader .= "Reply-To: ".$_POST["email"]."\r\n"; 
      $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
      $MESSAGE_BODY = "Name: ".$_POST["name"]."<br>"; 
      $MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>"; 
      $MESSAGE_BODY .= "Subject:".$_POST['subject']."<br />";  
      $MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"])."<br>"; 
      if(mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader))
       {
                echo "success ";
       }
     else
     {
       echo "Failure";
     }
   }


 ?>
</body>
</html>
0

All Articles