0

嗨,我对 php 完全陌生,我有一个网站,我有一个contact.html,它有可以填写姓名、电子邮件、保险类型、主题和评论的表格。点击提交按钮后,指定的电子邮件将收到详细信息。似乎无法定位问题所在。

这是contact.html的html代码

 <form id="ajax-contact-form">
        <div class="row">
            <div class="span4">
                <div class="form-padding">
                    <label for="f1">Your name(required):</label>
                    <input type="text" name="name" id="f1" class="form-text" value=""/>
                    <label for="f2">Insurance type:</label>
                    <input type="text" name="category" id="f2" class="form-text" value="" size="40"/>
                </div>
            </div>
            <div class="span4">
                <div class="form-padding">
                    <label for="f3">Your email(required):</label>
                    <input type="text" name="email" id="f3" class="form-text" value="" size="40"/>
                    <label for="f4">Subject:</label>
                    <input type="text" name="subject" id="f4" class="form-text" value="" size="40"/>        
                </div>
            </div>
        </div>
        <div class="row">
            <div class="span8">
                <div class="form-padding">
                    <label for="f5">Message(required):</label>
                    <textarea name="message" id="f5" cols="40" rows="10"></textarea>    
                </div>
            </div>
        </div>
                    <!-- send mail configuration -->
                    <input type="hidden" value="myemail@gmail.com" name="to" id="to" />
                    <input type="hidden" value="myemail@gmail.com" name="from" id="from" />
                    <input type="hidden" value="From contact form" name="subject" id="subject" />
                    <input type="hidden" value="send-mail.php" name="sendMailUrl" id="sendMailUrl" />
                    <!-- ENDS send mail configuration -->               
                    <p><input type="button" class="button red small" value="Submit Form" name="submit" id="submit" /></p>
        </form>

和我的 SEND-MAIL.PHP

    <?php
        //vars
        $subject = $_POST['subject'];
        $to = explode(',', $_POST['to'] );

        $from = $_POST['email'];

        //data
        $msg = "NAME: "  .$_POST['f1']    ."<br>\n";
        $msg .= "INSURANCE: "  .$_POST['f2']    ."<br>\n";
        $msg .= "EMAIL: "  .$_POST['f3']    ."<br>\n";
        $msg .= "SUBJECT: "  .$_POST['f4']    ."<br>\n";
        $msg .= "COMMENTS: "  .$_POST['f5']    ."<br>\n";

        //Headers
        $headers  = "MIME-Version: 1.0\r\n";
        $headers .= "Content-type: text/html; charset=UTF-8\r\n";
        $headers .= "From: <".$from. ">" ;


        //send for each mail
        foreach($to as $mail){
           mail($mail, $subject, $msg, $headers);
        }

        ?>

我的 JS 函数

// contact form
$("#ajax-contact-form").submit(function() {
var str = $(this).serialize();
    $.ajax({
    type: "POST",
    url: "send-mail.php",
    data: str,
    success: function(msg) {
        if(msg == 1) {
            result = '<div class="alert success fade in">Your message has been sent. Thank you!<a href="#" class="close-alert" data-dismiss="alert"></a></div>';
            $("#ajax-contact-form").hide();
        } else {result = msg;}
        $('#form-message').hide();
        $('#form-message').html(result);
        $('#form-message').fadeIn("slow");
        }
    });
return false;
});

我的问题

  1. 好像有错误,无法发送邮件。我很天真。任何帮助,将不胜感激。

  2. 如何在单击提交按钮后显示成功消息。

谢谢

4

1 回答 1

1

也许你必须完成你的表单打开标签

<form id="ajax-contact-form" method="POST" action="send-mail.php">

除非您使用 Ajax,否则您必须在问题中提供 ajax 代码。

于 2013-03-08T01:25:41.510 回答