-1

Hey so this is my first ever PHP contact form. Everything is working fine and I am receiving the emails, however the content of the email shows as "0".

My PHP code is:

$content = $_POST['name'] + $_POST['message'];

With name being the name of the form element (person's name), and message being the name of the message element. Both are displaying in the PHP array when I do print_r.

It's the email content that is the issue! I followed a Udemy tutorial on how to do this and have double checked everything. Can anyone help me??

Thanks!!!!

chris85
  • 23,846
  • 7
  • 34
  • 51
Grand
  • 175
  • 1
  • 12
  • 6
    `+` is *not* the concatenation operator in PHP – John Conde Jul 10 '17 at 20:38
  • 2
    Possible duplicate of [How to combine two strings together in PHP?](https://stackoverflow.com/questions/8336858/how-to-combine-two-strings-together-in-php) – chris85 Jul 10 '17 at 20:48

3 Answers3

3

In PHP you need to use "." in order to merge strings. "+" is for JS

Tzahi Serruya
  • 147
  • 2
  • 11
1

As others have noted, the "plus sign" does not concatenate strings; use the "dot" instead.

$content = $_POST['name'] . $_POST['message'];

Furthermore, the "plus sign" is causing the PHP interpreter to do type coercion, which is the reason your email literally says "zero". You've managed to turn your two POST variables into a number (although you're certainly not the first person to ever do so!)

Kevin_Kinsey
  • 2,285
  • 1
  • 22
  • 23
0

Judging by your submitted code, it looks like you're attempting to take the form submitter's name and their message and combine it into a single string that will be placed within the body of your email.

The primary issue that stands out is how you're attempting to concatenate (combine) the strings. In PHP, the concatination operation is the period ("."). With your current code, it's attempting to add the two strings like numbers, which is why you're encountering that 0 in your email body.

Let's take your current code, and apply that fix:

$content = $_POST['name'] . $_POST['message'];

Secondly, let's take a look at the actual output of your $content variable. Should I submit the name "Jon" and my message be "Hello, friends!" with your code as it is now, we would end up with:

JonHello,Friends!  

To remedy this, let's add in a colon and a space to denote that the user is saying this message:

$content = $_POST['name'] . ': ' . $_POST['message'];

Now, should we output the $content variable, we'll see:

Jon: Hello, Friends!

Bear in mind, our addition of the colon and space could be substituted for any other string.

For instance, we could modify our concatenation only slightly to add a bit of flavor to our text and have the user "say" her/his name:

$content = $_POST['name'] . 'says: "' . $_POST['message'] .'"';

This code, when output, would produce:

Jon says: "Hello, friends!"

I hope that helps, Grand. Let me know if you have any further questions.

Jonathan LeRoux
  • 413
  • 3
  • 12
  • 1
    Thank you very much for your detailed answer! Greatly appreciated. – Grand Jul 11 '17 at 00:06
  • Glad to be of help, Grand. If this helped answer your question, and you deem it to be the answer that best solved your problem, please consider marking it as your accepted answer to help other users who may have this question in the future. Cheers! Have a great day! – Jonathan LeRoux Jul 11 '17 at 05:47