Attempt to ) to a PHP page (with one parameter) is not working. The parameter passed in appears to be NULL?

Tom Knowlton
Tom Knowlton used Ask the Experts™
I have the following JavaScript making a call to a PHP page:

	function callPhpAPI(youdata) {
		$.post( "", { mes: youdata } );

Open in new window

PHP page:



    $emailto = '';
    $toname = 'Tom';
    $emailfrom = '';
    $fromname = 'xxx';
    $subject = 'activity on xxx detected:  ' + $mes;
    $messagebody = 'There was a page hit on xxx:  ' + $mes;
    $headers = 
        'Return-Path: ' . $emailfrom . "\r\n" . 
        'From: ' . $fromname . ' <' . $emailfrom . '>' . "\r\n" . 
        'X-Priority: 3' . "\r\n" . 
        'X-Mailer: PHP ' . phpversion() .  "\r\n" . 
        'Reply-To: ' . $fromname . ' <' . $emailfrom . '>' . "\r\n" .
        'MIME-Version: 1.0' . "\r\n" . 
        'Content-Transfer-Encoding: 8bit' . "\r\n" . 
        'Content-Type: text/plain; charset=UTF-8' . "\r\n";
    $params = '-f ' . $emailfrom;
    $test = mail($emailto, $subject, $messagebody, $headers, $params);
    // $test should be TRUE if the mail function is called correctly



Open in new window

The email IS being sent, but the PHP variable $mes that I am attempting to make use of has a problem.

The email when it arrives looks like this:


What I expected was to see the following subject and message body:

subject: 'activity on xxx detected:  content of param passed in
message body: 'There was a page hit on xxx:  content of param passed in

If I remove the $mes PHP variable I am trying to append to the end of the subject and message body (above) -- the subject and message body come through GREAT, minus the custom message(which is the entire point of all this)

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Fixer of Problems
Most Valuable Expert 2014
PHP uses '.' for text concatenation, not '+' (that's javascript).  This line..
$messagebody = 'There was a page hit on xxx:  ' + $mes;

Open in new window

Should probably be this:
$messagebody = 'There was a page hit on xxx:  '.$mes;

Open in new window

Most Valuable Expert 2017
Distinguished Expert 2018

You could also do this
$messagebody = "There was a page hit on xxx: {$mes}";

Open in new window

Double quotes allow for the inserting of variables within the string. The use of the {} is optional - it is usually used when variable insertion in the string creates ambiguity between what is string and what is variable. For example
$messageBoxy="There was a page hit on xxx: $meswhich was not good";

Open in new window

Where does the variable end and where does the rest of the string start. The {} removes the ambiguity.
$messageBoxy="There was a page hit on xxx: {$mes}which was not good";

Open in new window

I make a point of always enclosing my string variables in {}

If the message gets longer use HEREDOC
$messagebody = <<< MSG
There was a page hit on xxx:  {$mes}

Open in new window

HEREDOC is more versatile for blocks of text than a lot of string concatenations. It allows single and double quotes to be used without escaping and also supports variable embedding. In some cases it can result in neater and more manageable (easier to read) code.

I would also do a proper check of the mes parameter - don't assume it is there and I would avoid REQUEST you are posting to the script so check specifically for POST - don't give any malicious user any lattitude
$mes = isset($_POST['mes']) ? $_POST['mes'] : false;
if ($mes) {
   // we have a parameter so process it
else {
  // no parameter supplied - deal with it

Open in new window

Finally, I would recommend you take a look at PHPMailer - it has much better options than mail().

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial