在数据库和数据库中发送联系表格数据邮件

时间:2014-04-05 08:48:02

标签: javascript php mysql email

现在我正在使用数据库连接代码以及mail.php

我希望将联系人表单数据保存在数据库中(运行良好)以及将邮件发送到我的电子邮件地址(不知道如何使用manage_comments.php放置代码)

以下是我的联系方式:manage_comments.php,mail.php&的JavaScript

Plz帮助我将相同的数据保存在数据库中以及发送电子邮件

联系表单

    <form method='post' action="manage_comments.php">
  Name: <input type='text' name='name' id='name' />
  <div style="color:red;" id="nameerror"></div><br />

  Email: <input type='text' name='email' id='email' />
  <div style="color:red;" id="emailerror"></div><br />

  Contact: <input type='text' name='contact' id='contact' />
  <div style="color:red;" id="phoneerror"></div><br />


  <input type='submit' value='Submit' class="mailbtn" />  
</form>

manage_comments.php

<?php
if( $_POST )
{
  $con = mysql_connect("localhost","username","pwd");

  if (!$con)
  {
    die('Could not connect: ' . mysql_error());
  }

  mysql_select_db("my_db_name", $con);

  $users_name = $_POST['name'];
  $users_email = $_POST['email'];
  $users_contact = $_POST['contact'];


  $users_name = mysql_real_escape_string($users_name);
  $users_email = mysql_real_escape_string($users_email);
  $users_contact = mysql_real_escape_string($users_contact);


  $query = "
  INSERT INTO `my_db_name`.`table_name` (`id`, `name`, `email`, `contact`)
  VALUES ( Null, '$users_name', '$users_email', '$users_contact');";

  mysql_query($query);

  echo "<h2>Thank you for your Comment!</h2>";

  mysql_close($con);
}
?>

mail.php

<?php

$to =  array("email_Ad1","email_Ad2");

$subject = "My subject";
$message = "Inquiry from <b>".$_POST['name']."</b> and phone number is <b>".$_POST['phn']."</b>!";
$message .= "<br><br>";
$message .= "<table border='1'>";
$message .= "<tr><td>Name    </td><td>".$_POST['name']."</td></tr>";
$message .= "<tr><td>Phone   </td><td>".$_POST['phn']."</td></tr>";
$message .= "<tr><td>Email   </td><td>".$_POST['email']."</td></tr>";
$message .= "</table>";


$from = "other_email_Ad";

$headers = "MIME-Version: 1.0\r\n"; 

$headers .= "Content-type: text/html; charset=utf-8\r\n"; 

$headers .=  'from: '.$from .'' . "\r\n" .

            'Reply-To: '.$from.'' . "\r\n" .

            'X-Mailer: PHP/' . phpversion();



foreach($to as $row)
{
   mail($row,$subject,$message,$headers);
}

echo "Mail Sent.";
die;
?>

的javascript

<script type="text/javascript">

    $('.mailbtn').live('click',function(){


            name = $('#name').val();

            phn = $('#contact').val();

            email= $('#email').val();
---------validations----------------
$.ajax({
            type: "POST",
            async : true,
            url: "mail.php",
            data: { name:name, email:email, phn:phn}
            })
            .done(function( msg ) {
            $('.mail_middle').html('');
            $('.mail_middle').html('Thank you for quote request. One of the Flamingo Transworld team members will get back to you soon.');
            return false;
            });
</script>

1 个答案:

答案 0 :(得分:0)

您只需使用include,然后只需发布到manage_comments.php

<强> manage_comments.php

    [...]
    mysql_close($con);

    include "mail.php";
}