1
var data = $('#demoform').serialize();
var action = $('#demoform').attr('data-action');
console.log(data)
$.ajax({
  type: 'POST',
  url: '../include/demo.php',
  dataType: "json",
  data: {
    data: data,
    action: action
  },
  success: function(data) {

  }
}).done(function(data) {

});

In my php I get the data and I check it if the data is array like

$data =  $_POST['data'];
if (is_array($data)) {

}else{

}

I cant send it and when I use print_r(); I can get

task_date=11%2F30%2F2017+8%3A14+PM&task=qwe&task_person=qwe&task_status=0

But I always end up in the else part meaning the data is not an array.

What is the format of the data if not array?

Deckerz
  • 2,606
  • 14
  • 33
Lloyd
  • 27
  • 6

4 Answers4

0

The data that you provided is not an array. It is a key-value pair, that can be accessed like $data['task']='qwe'

0

You need to use urldecode() on your data that you receive.

This might be what you are interested in -

<?php
    $data = "task_date=11%2F30%2F2017+8%3A14+PM&task=qwe&task_person=qwe&task_status=0";
    parse_str($data, $output);
    print_r($output);
?>

Which Results in - Array ( [task_date] => 11/30/2017 8:14 PM [task] => qwe [task_person] => qwe [task_status] => 0 )

Tushar Walzade
  • 3,737
  • 4
  • 33
  • 56
0

In your case, $_POST["data"] is a url-encoded query string, see the jQuery manual:

Description: Encode a set of form elements as a string for submission.

You need parse_str() to parse the query string first and then you can use urldecode() on the individual values to get the original values back.

Edit: As noted by @splash58, the values are already decoded, see the manual on parse_str():

Note:

All variables created (or values returned into array if second parameter is set) are already urldecode()d.

So:

// Get the array
parse_str($_POST["data"], $data); 
jeroen
  • 91,079
  • 21
  • 114
  • 132
  • let me make it clear in the ajax i need to use `parse_str(data)` then in php i need to `urldecode($data)` ? then I use foreach now on them? I can check for array? – Lloyd Nov 30 '17 at 12:41
  • @Lloyd No, you need only `parse_str()` in php. Note that the results are set in the second parameter. – jeroen Nov 30 '17 at 12:44
  • @jeroen the problem is on js side with making data – splash58 Nov 30 '17 at 12:48
  • @splash58 It is unnecessarily complicated if you don't need to add extra fields, but I don't see a problem, you can send any string as the value. – jeroen Nov 30 '17 at 12:49
  • @jeroen Of course, your solution solve the problem. But OP will not get error source while using this code. From this point of view, it's better to change js and use internal PHP method to create correct $_POST – splash58 Nov 30 '17 at 13:03
0

As I see you have some problem variable of ajax request. Please update your code as below.

var data = $('#demoform').serialize();
var action = $('#demoform').attr('data-action');
console.log(data)
$.ajax({
  type: 'POST',
  url: action,
  data: data,
  success: function(data) {

  }
}).done(function(data) {

});

I hope this helps.

AppDevD
  • 143
  • 5