【问题标题】:Sending email - 1st time contact form creation发送电子邮件 - 第一次创建联系表
【发布时间】:2013-04-12 22:17:59
【问题描述】:

我的第一次创建自定义联系表单即将结束。我已经准备好了一切——我需要的只是 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"/>

我的按钮是一个动画函数(不是&lt;input type="button"/&gt;)。

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...");
    }
  });

问题:我应该把什么放在哪里?

【问题讨论】:

  • 请停止提供垃圾邮件脚本。谢谢!
  • @hakre,愿意提供更好的吗?这是我第一次……
  • 当然。想一想:如果您允许用户在网络表单中指定要向其发送电子邮件的电子邮件,则可以使用该表单将电子邮件发送到任何地方,而无需使用自己的计算机。相反,请考虑如何将允许的邮件地址减少到合理的最小值。
  • 对。我是php的菜鸟。你能指出我正确的方向吗?代码、教程……新手可以使用?
  • 这不是技术细节,而是更多地考虑哪些(以及如何)数据被传递。

标签: php jquery contact-form


【解决方案1】:

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

试试这个

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

【讨论】:

  • 感谢您的反馈。我进行了更改 - 我如何测试它是否有效?只能通过服务器...对吗?
  • 我看完了另一个教程。我更新了原始帖子中的代码。
猜你喜欢
  • 2018-04-15
  • 2020-05-20
  • 2019-10-03
  • 2020-05-05
  • 2020-12-07
  • 2011-05-30
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
相关资源
最近更新 更多