Reputation: 148
I am using this jQuery code to send data to PHP:
var fromdata2 = $('#form').serialize();
var file_data = $('#fileid').attr('files')[0];
var fromdata = new FormData();
fromdata.append('fileid', file_data);
fromdata.append('post_data', fromdata2);
$.ajax({
type: "POST",
cache: false,
contentType: false,
processData: false,
data: fromdata,
dataType: "json",
url: "url",
success: function (data)
{
alert("success");
}
});
I receive the data in this form:
code=&id=&CTR_ID=&ctr_name=asdsadsad&air_name=Action+Airlines&air_uniCode=XQ&ctr_strDate=04%2F11%2F2017&ctr_endDate=04%2F11%2F2017&ctr_docNameHid=5337XXXXXXXXXX78_09-06-2016&recstatus_val=1
But when I attempt to process it, with this code:
var_dump(unserialize($data));
I receive this error:
unserialize(): Error at offset 0 of 537 bytes.
How can i unserialize it, to find the result in an array, like this:
array(
code=>,
id=>,
CTR_ID=>,
ctr_name=>'asdsadsad'
)
Upvotes: 0
Views: 1983
Reputation: 5437
As said by @apokryfos in the comment, you can use parse_str
to parse the query strings like below:
$queryString = "code=&id=&CTR_ID=&ctr_name=asdsadsad&air_name=Action+Airlines&air_uniCode=XQ&ctr_strDate=04%2F11%2F2017&ctr_endDate=04%2F11%2F2017&ctr_docNameHid=5337XXXXXXXXXX78_09-06-2016&recstatus_val=1";
parse_str($queryString, $queryArray);
print_r($queryArray);
To upload multiple files:
var ins = document.getElementById('fileid').files.length;
for (var x = 0; x < ins; x++) {
fromdata.append("fileid[]", document.getElementById('fileid').files[x]);
}
Upvotes: 1
Reputation: 1362
You are trying to send JSON data but you give it form url-encoded data, which are two different things. If you want to send JSON just do something like this:
var form_data = $("#form").serializeArray()
form_data.push({fileid: $('#fileid').attr('files')[0]});
$.ajax({
type: "POST",
cache: false,
contentType: false,
processData: false,
data: fromdata,
dataType: "json",
url: "url",
success: function (data)
{
alert("success");
}
});
And then on PHP side just do json_decode(file_get_contents('php://stdin'), true);
Upvotes: 0