views:

49

answers:

1

Hi,

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

+4  A: 

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');
Aidan Kane
This is not the problem. He's named the textarea 'contactbody' so that should work OK.
ign
@Ignacio, I think it is the problem. It's not a problem with the jquery collecting the data from the field. It's that it's posting it through to the php by calling it 'message'.he could change: "message: $('#contactbody').val()," to: "contactbody: $('#contactbody').val()," in the jquery and it would work.thanks for the downvote though :)
Aidan Kane
Don't take it personal. A downvote is just "this answer is not useful" :)Anyway, I've read again and I think you're right. Sorry, mate.
ign
Ya, he was right, I modified it a bit to look like his 2nd comment. Thanks all.
tshauck