Help with Contact Form

This Contact Form submits and shows the “success” message, but the only thing that appears to be sent is:
name:
email:

If you can suggest why no other info is being sent, I’d greatly appreciate any guidance.

<script>
$(function () {
var form = $("#ajax-contact");
form[0].reset(); 
form.submit(function (event) {
event.preventDefault();
var data = {
"name": $("#contact-name").val(),
"email": $("#contact-email").val(),
"message": $("#contact-message").val()
};
$.ajax({
url: "../contact_form_handle.php", 
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
dataType: "text",
type: "POST"})
.done(function (response) {
$("#form-response").text(response);
form[0].reset(); 
})
.fail(function (data) {
if (data.responseText.length) {
$("#form-response").text(data.responseText);
} else {
$("#form-response").text("An error has occurred and your message could not be sent.");
}
})
});
});
</script>


<form id="ajax-contact" method="post">
<table class="table10">
<tr>
<td>
<label for="name">Your Name:</label>
<input id="contact-name" type="text" name="name" required>
</td>
</tr>
</tr>
<tr>
<td>
<label for="email">Your Email Address:</label>
<input id="contact-email" type="email" name="email" required>
</td>
</tr>
<tr>
<td>
<label for="message">Message:</label>
<textarea id="contact-message" type="text" name="message" required></textarea>
</td>
</tr>
<tr>
<td>
<button type="submit">Send</button>
</td>
</table>
</form>


<div id="form-response"></div>


<?php

$data = json_decode(file_get_contents("php://input"));
$name = trim($data->name);
$name = str_replace(array("\r", "\n"), array(" ", " "), $name);
$email = filter_var(trim($data->email), FILTER_SANITIZE_EMAIL);
$message = trim($data->message);
if (empty($name) || empty($message) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "One or more invalid entries. Please try again.";
exit;
}

$to = "support@...com";
$from = "From: contact-form@...com". "\r\n";
$email = $_POST['email'];
$name = $_POST['name'];
$message = $_POST['message'];

$message = "name: {$_POST['name']}\r\nemail: {$_POST['email']}\r\n\r\n{$_POST['message']}";

if (mail($to, "Customer Inquiry", $message, $from)) {

echo "Thank You. Your message has been sent.";
} else {
echo "An error has occurred and your message could not be sent.";
}
?>

What kinds of message are you sending it has any invalid characters it could not pass through if the email provider has any filtering applied.

Thanks for your reply.
I am just sending some test text

Someone suggested I comment-out a few lines in the php which I did (see below), because he said " don’t send POST vars" and “you are sending a JSON string”??? And now when I submit the Form, the info entered into the Form’s ‘message’ field sends/arrives only. No info from the Form’s ‘name’ field, nor from the Form’s ‘email’ field. He suggested something to do with the mail command. Here’s how it currently looks with the commenting:

$data = json_decode(file_get_contents("php://input"));
$name = trim($data->name);
$name = str_replace(array("\r", "\n"), array(" ", " "), $name);
$email = filter_var(trim($data->email), FILTER_SANITIZE_EMAIL);
$message = trim($data->message);
if (empty($name) || empty($message) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "One or more invalid entries. Please try again.";
exit;
}

$to = "support@...com";
$from = "From: contact-form@...com". "\r\n";
//$email = $_POST['email'];
//$name = $_POST['name'];
//$message = $_POST['message'];

//$message = "name: {$_POST['name']}\r\nemail: {$_POST['email']}\r\n\r\n{$_POST['message']}";

if (mail($to, "Customer Inquiry", $message, $from)) {

echo "Thank You. Your message has been sent.";
} else {
echo "An error has occurred and your message could not be sent.";
}

Any help with getting the Form field info from ‘name’ and ‘email’ to send/arrive will be appreciated.

Well you according your to your code you commented out the email and name field so they technically the do not exists.

As for your code i do not see any code that stands out as failure. One thing that kinds has me i guess concerned is in your message the new line chars run intro your string chars. Which depending on the client could present a problem but this is unlikely.

I would suggest trying to send a header along with your email that will define the email content type which could be the problem.

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

if(mail($to, $subject, $message, $headers))
{
    echo "Thank You. Your message has been sent.";
} 

else 
{
     echo "An error has occurred and your message could not be sent.";
}

Thanks for your reply.

However, with or without the “headers” addition, the Contact Form only sends the info from the ‘message’ field.

Any additional help will be appreciated.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.