1

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

我的按钮是一个动画功能(不是 a <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...");
    }
  });

问题:我在哪里放什么?

4

1 回答 1

1

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

尝试这个

 $.ajax({
  ...
 data: $('#yourFormID').serialize(),
 success: function(){
  ...
于 2013-04-19T13:42:56.617 回答