tshauck
tshauck

Reputation: 21574

PHP mail function + ajax = frustrated me

I'm using ajax to submit a contact from without reloading the page, it works well for the most part except when I try to send the body of the message nothing gets sent. The to and subject parts work fine, it is just when the body tries to get sent I see nothing. I've tested it running strickly a php function and the body works, just the page reloads and I'm not sure why it works here, but not with ajax. If anybody could shed some light it'd be great, thanks.

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

Here is the php function (using CI, btw)

function sendemail(){
        $to = "[email protected]";
        $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();

    }

And the form if that helps

    <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">[email protected]</p>
                <input id ="subject" type="text" name="subject" /><br/>
                <textarea id = "contactbody" name="contactbody"></textarea>
            </div>
        </form>
   </div>
</div>

Thanks for the help

Upvotes: 0

Views: 1347

Answers (1)

Aidan Kane
Aidan Kane

Reputation: 4006

in the jquery you're sending the message using the variable 'message' but in the php you're picking it up using 'contactbody'.

change the php from:

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

to:

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

Upvotes: 4

Related Questions