Scenario
From Js I create a dynamic FormData Object and then I post the data with $.ajax() to a PHP.
Problem
I can't access the variables in PHP
var_dump($_POST['name']); // This returns NULL
NULL
But! this shows the Array(), so the data is there
var_dump($_POST); // This shows the full array()
array(1) {
["------WebKitFormBoundarybLgHYUQw1augDdyF
Content-Disposition:_form-data;_name"]=>
string(161) ""author"
test
------WebKitFormBoundarybLgHYUQw1augDdyF
Content-Disposition: form-data; name="name"
jon snow
------WebKitFormBoundarybLgHYUQw1augDdyF--
"
}
Edit: This works too
foreach($_POST as $key=>$value)
{
echo "$key=$value";
}
Log:
------WebKitFormBoundarycozyxlP96SxBy3O0
Content-Disposition:_form-data;_name="author"
test
------WebKitFormBoundarycozyxlP96SxBy3O0
Content-Disposition: form-data; name="name"
jon snow
------WebKitFormBoundarycozyxlP96SxBy3O0--
update
foreach($_POST as $key=>$value)
{
var_dump($key);
}
?>
Log $key:
string(78) "------WebKitFormBoundaryK2sJT1BCmNt5jVVW
Content-Disposition:_form-data;_name"
//
foreach($_POST as $key=>$value)
{
var_dump($key);
}
?>
Log $value;
string(161) ""author"
test
------WebKitFormBoundaryosBAfp5AzD7g7fly
Content-Disposition: form-data; name="name"
jon snow
------WebKitFormBoundaryosBAfp5AzD7g7fly--
The Js methods
Rimodromo.prototype.collectData = function(audio, sound, author) {
var newPoemStore = rimodromo.newPoem;
formData = new FormData();
// formData.append("rime", newPoemStore);
// formData.append("audio", new Blob([recorder.blob], {type:"application/octet-stream"}));
// formData.append("sound", rimodromo.selectedSound);
formData.append("author", "test");
formData.append("name", "jon snow");
// var myData = {
// rime:newPoemStore,
// audio:recorder.blob,
// sound:rimodromo.selectedSound,
// author:author || 'anónimo',
// name:name || 'nombre de rima'
// }
// return myData;
return formData;
};
Rimodromo.prototype.submitRimodromo = function() {
var myData = rimodromo.collectData();
for (var pair of myData.entries()) {
console.log(pair[0]+ ', ' + pair[1]);
}
$.ajax({
url : "insert.php",
type: "POST",
data: myData,
cache: false,
contenType: false,
processData: false,
success: function(data,status,xhr) {
console.log(data);
console.log(status);
}
});
};
Any clues? Thanks in advance =)