I've looked through several question/answer combinations relating to my issue, but the suggestions I've tried haven't worked.
This is my my AJAX request:
$('#submitRequest').on('click', function() {
// package everything to get it ready for transfer
var textSamples = JSON.stringify(packTextStyles());
var swatches = JSON.stringify(packSwatches());
var clientInfo = JSON.stringify(packClientInfo());
$.ajax ({
type: 'post',
url: 'classes/requestHandler.php',
//headers: {'Content-Type': 'application/json'},
data: {
typeSamples: textSamples,
swatchSamples: swatches,
userInfo: clientInfo
},
success: function(data) {
if (data === 'added') {
alert('ok');
}
else {
alert(data);
return false;
}
}
});
return false;
});
The three vars textSamples
, swatches
, and clientInfo
are all arrays. The textSamples
array contains eight objects with each having five properties, so it's a good chunk of data being sent over.
My problem is that var_dump($_POST)
returns an empty array and any attempt at trying json_decode($_POST['typeSamples'])
gives me an error of undefined index on PHP side.
Here is the screenshot of the headers on the network tab in Chrome dev tools:
So $_POST is getting something, but I have no idea where it's going. Some questions:
Do I need to put
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
orheaders: {'Content-Type': 'application/json'}
in the AJAX, since I usedJSON.stringify()
?Because I used AJAX I didn't put
method="post" action="something.php"
in theform
tag, and theinput
tags don't have thename
attributes.Submit
button doesn't havetype="submit"
. Does that matter?
EDIT: If it matters, I'm using jQuery 1.11.1 and PHP 5.4.12
EDIT2: half-finished PHP:
<?php
$reqGetter = new RequestReceiver;
$reqGetter->checkInput();
class RequestReceiver{
// public function __construct() {
// $textSamples = json_decode($_POST['typeSamples']);
// }
public function checkInput() {
if (!empty($_POST)) {
echo('added');
$typeSamples = json_decode($_POST['typeSamples'], true);
print_r($typeSamples);
} else {
echo('failed');
}
}
}