Jump to content

Contact Form sends only 'message' field info only ...


Chrisj

Recommended Posts

When 'submit' is selected in this Contact Form, the 'message' field info is the only thing that sends/arrives. Someone suggested it may have to do with the 'mail command', but any help with getting the 'name' and 'email' field info to send will be appreciated.

<?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";

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

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

Here's more code:

<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>


<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>
Edited by Chrisj
Link to comment
Share on other sites

You are only sending the message as the e-mail content, so how exactly do you expect to receive the name and e-mail address? PHP only does what you tell it to do.

 

You either have to actually put the contact data into the e-mail content, e. g.:

name: <insert name here>
e-mail address: <insert address here>
message: <insert message here>

Or you add them to a Reply-To header so that they show up in your e-mail program.

 

In any case, the mail() function sucks and is going to cause security problems as soon as you start adding dynamic headers. Use a proper mailer library like PHPMailer.

 

The whole JSON stuff doesn't make a lot of sense either. You can skip all the encoding and decoding if you use plain old default form encoding.

Edited by Jacques1
Link to comment
Share on other sites

Thanks for your replies.

This is not code that I wrote. Someone provided it to me and it seems like it's close to working.

I'm not familiar with "superglobal $_PRINT".

And I'm not clear on "put the contact data into the e-mail content", doesn't the Contact Form user enter the content into the Form fields?

And additional guidance/examples, would be appreciated

Link to comment
Share on other sites

The user fills out the contact form and submits three pieces of data: The username, the e-mail address and the message. What you do with this data is your own responsibility. Right now, you're only using the submitted message as the e-mail content. Everything else gets dropped. It doesn't show up anywhere.

 

If you haven't written the code, you'll at least need to read and understand it. Otherwise there isn't really much we could do.

Link to comment
Share on other sites

Thanks for your replies.

Any additional help will be appreciated.

 

You said:

When 'submit' is selected in this Contact Form, the 'message' field info is the only thing that sends/arrives.

 

What does that mean to you?  It means to me...

  1. First, I should do one more (or even more) reality checks of what the client is sending to the server to ensure what I am witnessed is reality.  80% of the time it isn't.
  2. Assuming 1 passes, this has nothing to do with the server (unless it is supplying faulty HTML), and I should be looking at the client (i.e. the form and/or js).  I shouldn't be going down a rabbit hole troubleshooting the email class as it is not the issue, but should focus solely on the client.
  3. That I should definitely vote against Trump as the script will not run correctly if he is elected.
Link to comment
Share on other sites

Guys, read the code:

$data = json_decode(file_get_contents("php://input"));
// ...
$message = trim($data->message);
// ...
mail($to, "Customer Inquiry", $message);

How exactly are the name and e-mail address supposed to get to the receiver? They're not in the e-mail content, they're not in any header, they're just discarded in the script.

 

PHP doesn't magically add data to e-mails. You have to actually do it yourself, and I've already explained how.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.