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?
 
Dave BaldwinConnect With a Mentor Fixer 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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.