/    Sign up×
Community /Pin to ProfileBookmark

Contact Form is not sending all the information

Contact Form is not sending ‘$message’, ‘$email’, or ‘$name’, just ‘$from’ info arrives
Here’s the code:

[CODE]<?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”. “rn”;
$email = $_POST[’email’];
$name = $_POST[‘name’];
$message = $_POST[‘message’];

if (mail($to, $message, $from, $email, $name )){
echo “Thank You. Your message has been sent.”;
} else {
echo “An error has occurred and your message could not be sent.”;
}
?>[/CODE]

to post a comment
PHP

20 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 19.2016 — Everything you want in the actual message body of the email needs to be in the 3rd argument to mail (see http://php.net/mail ):
[FONT=Courier New]bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )[/FONT][/quote]
Therefore you'll want to append those values to the $message variable in your code, as opposed to making them additional arguments to mail() -- and that then needs to be the 3rd argument, not the second (which is the subject value). So it might be something like:
[code=php]
$message = "Name: {$_POST['name']}rnEmail: {$_POST['email']}rnrn{$_POST['message']}";
if (mail($to, "Customer Inquiry", $message, $from)) {
[/code]
Copy linkTweet thisAlerts:
@chrisjchrisjauthorOct 19.2016 — Thank you for your reply. I've added in your suggestions like so:

[CODE]
<?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 = "[email protected]";
$from = "From: [email protected]". "rn";
$email = $_POST['email'];
$name = $_POST['name'];
$message = $_POST['message'];

[B]$message = "Name: {$_POST['name']}rnEmail: {$_POST['email']}rnrn{$_POST['message']}";[/B]

[B]if (mail($to, "Customer Inquiry", $message, $from)) {[/B]

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


And now I see this in the email that arrives:

Name:

Email:

but, no info that was entered into a Form field.

Any additional help as to how to show Form field info, will be appreciated.
Copy linkTweet thisAlerts:
@NogDogOct 19.2016 — First step would be to make sure that $_POST has the expected field names. (No guarantee my guesses were correct. ? ). A quick and dirty check just for debugging somewhere near the top of the script:

[code=php]
die("<pre>".print_r($_POST, 1)."</pre>");
[/code]

Remember that those array keys/field names are case-sensitive.
Copy linkTweet thisAlerts:
@chrisjchrisjauthorOct 19.2016 — Much thanks again for your reply/help.

I looked and believe the field name match the php.

I added your debug code suggestion and see this:

[CODE]<pre>Array ( ) </pre>[/CODE]

Here is more Form code:


[CODE]
<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.r
Copy linkTweet thisAlerts:
@chrisjchrisjauthorOct 19.2016 — <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>
Copy linkTweet thisAlerts:
@NogDogOct 20.2016 — So, that empty array in the debug output is telling me nothing got sent to the PHP script. Being primarily a server-side dev, I'll leave it to someone else to wade through the JavaScript and figure out why it's not sending anything. ? (Personally, I'd have just used a HTML form, but that's just me.)
Copy linkTweet thisAlerts:
@chrisjchrisjauthorOct 20.2016 — Thanks for your reply.

I began with an html form, but could not figure out how to stay on the same page after 'submit', and show the 'sent message'
Copy linkTweet thisAlerts:
@ginerjmOct 20.2016 — If your html form uses php vars in its value attibutes AND if your php script retrieves those same php var values from the POST input then your process can simply do its processing and re-send the same html page back (if necessary), with those same php vars in the value attributes, along with a message field. I do it all the time.
Copy linkTweet thisAlerts:
@chrisjchrisjauthorOct 20.2016 — Thanks for your message.

I'm not sure if it's doing all those things.

Here's the Form code. Any help will be appreciated:

[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>[/CODE]
Copy linkTweet thisAlerts:
@ginerjmOct 20.2016 — You need to add the value attributes to your inputs. Then in you script when you grab those values from $_POST, store them in those same php vars that you use in the value attributes.
Copy linkTweet thisAlerts:
@chrisjchrisjauthorOct 20.2016 — Thanks for your reply. Greatly appreciated.

Regarding "add the value attributes to your input". I'm not clear on that.

Aren't I already doing that with:
[CODE] name="name"
name="email"
name="message"[/CODE]


and this?

[CODE]$email = $_POST['email'];
$name = $_POST['name'];
$message = $_POST['message']; [/CODE]


I guess I don't follow what you're saying.

Any clarification will be appreciated.
Copy linkTweet thisAlerts:
@ginerjmOct 20.2016 — Close. What you are showing is the name attributes of your html. You need to add the value ones. And those values should then use the php vars that you are saving in the php code you showed.
Copy linkTweet thisAlerts:
@chrisjchrisjauthorOct 20.2016 — Thank you for your reply.

Can you provide an example of what you mean, please?
Copy linkTweet thisAlerts:
@ginerjmOct 20.2016 — Do you not have any resource for looking up an html tag?

<i>
</i>&lt;input type='text' name='input1' value='$input1'&gt;";


This allows you to output the tag with the value of $input1 displayed in it.

[code=php]
$input1 = $_POST['input1'];
[/code]

This is how you get the value of the tag.
Copy linkTweet thisAlerts:
@chrisjchrisjauthorOct 20.2016 — Thanks for your reply.

I already have:
[CODE]<input id="contact-name" type="text" name="name" required>[/CODE]
and
[CODE]$name = $_POST['name'];[/CODE]

When I add in the value= it fills in the field, which is supposed to be empty for the User to populate.

So, I'm not understanding, apparently.
Copy linkTweet thisAlerts:
@NogDogOct 20.2016 — I think things have gotten spaghettified here. There are separate but interacting pieces here that should be addressed separately.

  • 1. Making sure your form-handler will send the email you want it to send when it actually receives form data.


  • 2. Making sure your web page actually sends the desired form data when you want it to.


  • Here in the PHP sub-forum, we can help you with #1. #2 as currently implemented on your web page is a JavaScript issue, as you appear to be using jQuery to submit the form via an AJAX request. If you want to address why your form is apparently not actually sending anything in a POST request to your form-handler, you're probably better off having the fine folks in the JavaScript sub-forum here take a look at it and see if they can help.

    On top of that, we seem to have gotten off on a tangent regarding not using JavaScript at all for this, which has its pros and cons. Certainly these days "single-page apps" are all the rage with lots of (often complex) JavaScript running on the browser side. Whether that's really needed in this case or not is yet another separate question we could debate (and probably never reach a consensus on).
    Copy linkTweet thisAlerts:
    @chrisjchrisjauthorOct 20.2016 — Thanks for your message.

    Are you saying I should re-post this in Javascript section?
    Copy linkTweet thisAlerts:
    @ginerjmOct 20.2016 — Following up on our tangential conv. make sure that the first time you send the page (or anytime you don't want to see the values again) you set those php vars to ''
    Copy linkTweet thisAlerts:
    @chrisjchrisjauthorOct 20.2016 — Thank you for all of your replies, but I don't understand what you're telling me.
    Copy linkTweet thisAlerts:
    @NogDogOct 20.2016 — Thanks for your message.

    Are you saying I should re-post this in Javascript section?[/QUOTE]


    If you want to use JavaScript to submit your form, then yes, I'd suggest you post there to find out what you need to change/fix in order to get it to actually submit (via the POST method) the form fields to your PHP script. Keep it focused to that problem: getting the form data to the server. Once you solve that, either the earlier suggestions we made to actually get the PHP mail() function to work for you will actually work, or we'll have to debug further if not -- but if you can't get the form data to the PHP script in the first place, then anything we try on the PHP side will be meaningless.
    ×

    Success!

    Help @chrisjchrisj spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.13,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...