Steve Ontologic
Steve Ontologic

Reputation: 199

PHP mail not sending form data with ajax. Data is blank in email

I'm working on a simple signup form in a codeignitor install.

The email is getting sent, but the form data isn't showing up in the email message. The data is sent via AJAX to the welcome file and hits the public function signup().

It's hitting the file, since the email is getting delivered, but no form data.

FORM:

<div class="form">
                                <form ONSUBMIT="return checkRequired(this)" method="post" id="subForm">
                                    <div class="signupbox">
                                         <span class="label"><label for="FirstName">First Name:</label></span>
                                            <span><input type="text" name="firstname" id="firstName" size="25" class="required" /></span>
                                    </div>
                                    <div class="signupbox">
                                        <span class="label"><label for="LastName">Last Name:</label></span>
                                            <span><input type="text" name="lastname" id="lastName" size="25" class="required" /></span>
                                    </div>
                                    <div class="signupbox">
                                        <span class="label"><label for="email">Email:</label></span>
                                            <span><input type="text" name="email" id="email" size="25" class="required" /></span>
                                    </div>
                                    <div class="signupbox">
                                        <span class="label"><label for="Company">Company:</label></span>
                                            <span><input type="text" name="company" id="company" size="25" /></span>
                                    </div>
                                    <div class="signupbox">
                                        <span class="label"><label for="Comments">Tell us about yourself:</label></span>
                                            <span><textarea name="comments" id="comments" cols="45" rows="5" ></textarea></span>
                                    </div>
                                    <div>
                                        <span class="button"><input type="submit" value="Sign Up" id="ajaxsubmit" /></span>
                                    </div>
                                </form>
                            </div>  

signup.js:

$(document).ready(function(){

$('#ajaxsubmit').click(function(){


    var formobj = new Object();

    formobj.firstName = $('#firstName').val();
    formobj.lastName = $('#lastName').val();
    formobj.email = $('#email').val();
    formobj.company = $('#company').val();
    formobj.comments = $('#comments').val();







    //alert(JSON.stringify(formobj));


            $.ajax({
              type: 'post',
              url: 'http://www.alloi.co/signup',
              dataType: 'json',
              data: JSON.stringify(formobj) 
              });
    })


});

welcome.php:

public function signup()
{


    $fname = $this->input->post('firstname', TRUE);
    $lname = $this->input->post('lastname', TRUE);
    $email = $this->input->post('email', TRUE);
    $company = $this->input->post('company', TRUE);
    $comments = $this->input->post('comments', TRUE);
    $headers = "MIME-Version: 1.0";
    $headers = "Content-type: text/plain; charset=iso-8859-1";
    $headers = "X-Mailer: PHP/".phpversion();

    $message = $fname." ".$lname. " has signed up for COMPANY.\n\nYou can reach ".$fname." at ".$email."\n\n".$fname." works for ".$company. "\n\n \n\n" .$comments;        

    mail("[email protected]", 'Alloi Account Sign-up', $message, $headers);
}

Email message:

has signed up for ALLOI.

You can reach at

works for

I'm sure it's something simple, but I'm still learning. What am I doing wrong to not send the form data?

Thank you very much in advance,

Steven

Upvotes: 1

Views: 700

Answers (1)

Musa
Musa

Reputation: 97707

You are sending json in your post but you try to read the data as application/x-www-form-urlencoded on the server side, If you want to read the data as is on the server side just pass the formobj as is

$.ajax({
    type: 'post',
    url: 'http://www.alloi.co/signup',
    data: formobj
});

Upvotes: 1

Related Questions