2

I was working on a simple link that would initiate an ajax call. This worked as expected until our Ubuntu server got decommissioned; we have a new, 'identical' server. The problem occurs on the server side, when I examine what is in POST. Expected echo of $_POST["scan_date"] is: 2012-11-26

Actual echo is: 2012-11-26scan_date=2012-11-26

And this is the actual echo in the PHP code below. The client side alert gives this: 2012-11-26 and that is what i expect. So, it appears that something is happening on the server side. I expect the echo to be "2012-11-26" and not "2012-11-26scan_date=2012-11-26"

I can't figure out why the POST data is getting munged up like that.

PHP:

$date = isset($_POST["scan_date"]) ? $_POST["scan_date"] : date("Y-m-d");
echo $date;  //produces 2012-11-26scan_date=2012-11-26 which is bad

JQuery:

var val = $(this).attr('value');
dateSelected = val.replace(/facterOption-/g, "");
dateSelected = $.trim(dateSelected);
alert(dateSelected);  //alerts 2012-11-26 which is good

$.ajax({
type: "POST",
dataType: 'json',
url: 'https://someurl',
data: {
scan_date: dateSelected
}
})

UPDATE: We have tried command line curl calls that were completely outside of the code framework for this PHP project, and the same result is being noticed. So maybe Apache has something to do with this.

Additionally, we discovered that adding a second POST parameter and value took care of this.

user1082428
  • 1,081
  • 3
  • 12
  • 14
  • Why don't you use $.post instead of $.ajax ? It certainly won't resolve your problem but is "easier to understand and use" http://docs.jquery.com/Ajax/jQuery.ajax – Mathieu Lordon Nov 26 '12 at 22:42
  • 1
    What does `echo file_get_contents('php://input')` say? That's basically what PHP receives before it decodes it into `$_POST`. – Ja͢ck Nov 26 '12 at 23:59
  • Jack, this is interesting, here is what I got from your suggestion: scan_date=2012-11-09scan_date=2012-11-09 – user1082428 Nov 27 '12 at 00:19
  • Definitely strange; could be an Apache issue then. – Ja͢ck Nov 27 '12 at 00:44
  • Agreed. And we have noticed this only occurs when we POST with just one parameter. When we POST with two parameters, it is fine. – user1082428 Nov 27 '12 at 01:00
  • Take a look at [this post](http://stackoverflow.com/questions/989967/best-way-to-log-post-data-in-apache), maybe it will help in debugging. – Ja͢ck Nov 27 '12 at 01:36

3 Answers3

1

This is an existing bug.

https://bugs.php.net/bug.php?id=22773

Solution is to:

  • Send another dummy name/value pair into the post
  • require > IE6
  • Use GET instead of POST if applicable
KingKongFrog
  • 13,946
  • 21
  • 75
  • 124
1

In your PHP file, try changing:

echo $date;

to

echo $_POST["scan_date"];

just to see what the PHP file is receiving.

cssyphus
  • 37,875
  • 18
  • 96
  • 111
1

Chrome developer tools network tab or Firebug for Firefox will help you to debug the requests. Take a look at the request that is being sent. It is unlikely that there's something in PHP that would append stuff to your request. Most likely it's in JavaScript.

valentinas
  • 4,277
  • 1
  • 20
  • 27