发送电子邮件 - 第一次联系表单创建

时间:2013-04-19 13:39:40

标签: php jquery contact-form

我即将结束第一次创建自定义联系表单。我已经准备好了 - 我需要的只是php / ajax代码。谷歌搜索后,这就是我所拥有的:

输入字段(HTML文件):

<label for="userName">Name:</label>
<input id="userName" class="inputField" type="text" name="userName"/>

<label for="userEmail">E-mail:</label>
<input id="userEmail" class="inputField" type="text" name="userEmail"/>

<label for="userSubject">Subject:</label>
<input id="userSubject" class="inputField" type="text" name="userSubject"/>

<label for="userMessage">Message:</label>
<textarea id="userMessage" class="inputField" rows="" cols="" name="userMessage" style="overflow:hidden"></textarea>

<label for="userSubject">Assunto:</label>
<input id="userSubject" class="inputField" type="text" name="userSubject"/>

我的按钮是一个动画功能(不是 <input type="button"/>)。

PHP文件(send.php):

<?php

    // Main Variables Used Throughout the Script
    $domain = "http://" . $_SERVER["HTTP_HOST"]; // Root Domain - http://example.com
    $siteName = "wwwnovaplaquemar.pt";
    $siteEmail = "guida.e.pedro@gmail.com";
    $er = "";

    // Check if the web form has been submitted
    if (isset($_POST["userEmail"])){
        // Process the web form variables
        // Strip out any malicious attempts at code injection, just to be safe.
        // All that is stored is safe html entities of code that might be submitted.
        $userName = htmlentities(substr($_POST["userName"], 0, 100), ENT_QUOTES);
        $userEmail = htmlentities(substr($_POST["userEmail"], 0, 100), ENT_QUOTES);
        $userSubject = htmlentities(substr($_POST["userSubject"], 0, 100), ENT_QUOTES);
        $userMessage = htmlentities(substr($_POST["userMessage"], 0, 10000), ENT_QUOTES);


        // Perform some logic on the form data
        // If the form data has been entered incorrectly, return an Error Message

            // Prepare the E-mail elements to be sent
            $subject = $userSubject;
            $message = 
            '<html>
                <head>
                <title>' . $siteName . ': A Contact Message</title>
                </head>
                <body>
                ' . wordwrap($userMessage, 100) . '
                </body>
            </html>';

            // We are sending the E-mail using PHP's mail function
            // To make the E-mail appear more legit, we are adding several key headers
            // You can add additional headers later to futher customize the E-mail

            $to = $siteName . ' Contact Form <' . $siteEmail . '>';

            // To send HTML mail, the Content-type header must be set
            $headers = 'MIME-Version: 1.0' . "\r\n";
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

            // Additional Headers
            $headers .= 'From: ' . $userName . ' <' . $userEmail . '>' . "\r\n";
            $headers .= 'Reply-To: ' . $userName . ' <' . $userEmail . '>' . "\r\n";
            $headers .= 'Return-Path: ' . $userName . ' <' . $userEmail . '>' . "\r\n";
            $headers .= 'Date: ' . date("r") . "\r\n";
            $headers .= 'X-Mailer: ' . $siteName . "\r\n";

            // And now, mail it
            if (mail($to, $subject, $message, $headers)){
                echo '<div>Thank you for contacting ' . $siteName . '. We will read your message and contact you if necessary.</div>';
            }
            else {
                $er .= 'We weren&#39;t able to send your message. Please contact ' . $siteEmail . '.<br />';
            }
    }

?>

在我的js文件中:

$.ajax({
    type: "POST",
    url: "send_pt.php",
    data: data,
    success: function(){
      $('#inputText_infoBox p').html("A enviar e-mail...");
    }
  });

问题:我在哪里放什么?

1 个答案:

答案 0 :(得分:1)

假设click事件正在调用ajax函数..你需要在ajax中使用data将数据发送到服务器。使用jquery.serialize将表单序列化..这会将所有表单元素发布到服务器

试试这个

 $.ajax({
  ...
 data: $('#yourFormID').serialize(),
 success: function(){
  ...