简体   繁体   中英

Ajax not posting form data

I recently asked a question about executing a Send Mail script without a page reload and worked out what my problem was with my AJAX, that's all resolved. I now have an issue with the following:

When my form posts to my AJAX the script is executed, however the post data doesn't seem to be come through to the PHP script.

The link to my question is: Contact Form same page success .

Here is my PHP:

<?php


$name =  ($_POST['name']);
$email =  ($_POST['email']);
$fsubject =  ($_POST['subject']);
$message = ("Name: ". $name . "\nEmail Address: " . $email . "\n\nMessage: " . $_POST['message']);


// Set Mail
$to = "emailaddress@fakeone.com";
$header = 'From: contactus@fakeone.com' . "\r\n" .
    'Reply-To: website@fakeone.com';
$subject = "{$fsubject}";
$body = "{$message}";


// Send Mail
if (mail($to, $subject, $body, $header)) 
{
    echo("<p>Message successfully sent!</p>");
} 
else 
{
    echo("<p>Message delivery failed...</p>");
}

?>

My HTML

<div id="success" style="color:red;"></div>

<form action="" id="contactform" method="post">
<div class="row">
<div class="form-group">
<div class="col-md-6">
<label>Your name *</label>
<input type="text" value="" data-msg-required="Please enter your name." maxlength="100"     class="form-control" name="name" id="name">
</div>
<div class="col-md-6">
<label>Your email address *</label>
<input type="email" value="" data-msg-required="Please enter your email address." data-msg-email="Please enter a valid email address." maxlength="100" class="form-control" name="email" id="email">
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<label>Subject</label>
<input type="text" value="" data-msg-required="Please enter the subject." maxlength="100" class="form-control" name="subject" id="subject">
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<label>Message *</label>
<textarea maxlength="5000" data-msg-required="Please enter your message." rows="10"     class="form-control" name="message" id="message"></textarea>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<input type="submit" id="submit" value="Send Message" class="btn btn-info btn-lg" data-loading-text="Loading...">
</div>
</form>

My AJAX

<script>
        $(document).ready(function () {

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

                $.post("sendmail.php", $("#contactform").serialize(), function (response) {
                    $('#success').html(response); 

                });
                return false;
            });

        });
    </script>

When I fill out the forms, nothing comes back it sends an empty email.

Any idea's why the post isn't working on this would be greatly appreciated. If I am breaking any rules by posting another question please let me know!

Regards, Dan

I think the problem is in your PHP code. You've got a variable named $headers , but in your mail function you're using $header . A typo.

Change the following lines from this -

$subject = "{$fsubject}";
$body = "{$message}";

to this -

$subject = $fsubject;
$body = $message;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM