代码之家  ›  专栏  ›  技术社区  ›  tshauck

PHP mail function+ajax=me

  •  0
  • tshauck  · 技术社区  · 16 年前

    js先生

    $(document).ready(function () {
    
        $('#submit').click(function () {
    
            var contactformdata = {
                you: $('#you').val(),
                subject: $('#subject').val(),
                message: $('#contactbody').val(),
            }
    
    
            $.ajax({
                url: "http://www.trenthauck.com/index.php/home/sendemail",
                type: 'POST',
                data: contactformdata,
                success: function () {
                    $('#contactheader').replaceWith("<p class='header'>Thanks</p>");
                    $('#contactform').remove();
                    $('#contactlink').remove();
                    $(document).scrollTop(25);
                }
            });
    
            return false;
        });
    });
    

    function sendemail(){
            $to = "auck@gmail.com";
            $from = $this->input->post('you');
            $subject = $this->input->post('subject');
            $message = $this->input->post('contactbody');
    
            $tosend = "From: " . $from . "\nMessage: " . $message;
    
            mail($to, $subject, $message);
    
            $this->index();
    
        }
    

    如果有帮助的话

        <div class="divider" id="contact">
        <p class = "header"><a id="contactheader" name="gocontact">Contact</a></p>
        <div id = "contactform">
            <form method = "post" id="contactform" action="<?php site_url()?>index.php/home/sendemail">
                <div id ="formtitles">
                    <p class = "info">You:</p>
                    <p class = "info">Me:</p>
                    <p class = "info">Subject:</p>
                    <p class = "info">Body:</p>
                    <input id = "submit" type="submit" value="Send" />
                </div>
                <div id ="formfields">
                    <input id="you" type="text" name="you" /><br/>
                    <p class = "info">auck@gmail.com</p>
                    <input id ="subject" type="text" name="subject" /><br/>
                    <textarea id = "contactbody" name="contactbody"></textarea>
                </div>
            </form>
       </div>
    </div>
    

    谢谢你的帮助

    1 回复  |  直到 13 年前
        1
  •  4
  •   Aidan Kane    16 年前

    在jquery中,您使用变量'message'发送消息,但在php中,您使用'contactbody'接收消息。

    将php更改为:

    $message = $this->input->post('contactbody');
    

    收件人:

    $message = $this->input->post('message');
    
    推荐文章