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

I have the following JavaScript making a call to a PHP page:

	function callPhpAPI(youdata) {
	
		$.post( "http://www.utahkidsfoundation.com/recsoap.php", { mes: youdata } );
	
	}

Open in new window



PHP page:

<?php

$mes=$_REQUEST['mes'];


    $emailto = 'xxx@gmail.com';
    $toname = 'Tom';
    $emailfrom = 'xxxr@xxx.com';
    $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

    header("Location: http://xxx.com");

    die();
?>

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:

zero

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)

zero
LVL 5
Tom KnowltonWeb developerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
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

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Julian HansenCommented:
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}
MSG;

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
<?php
$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().
2
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.