1

I send a data like page number to a php page to do some things. Then php code sends me an array. To get php array in jQuery I used json. It has returned an array but in php page it can't get the page number that I send?

jQuery:

      $.ajax({
        type: 'POST',
        url: 'php/pagging_employs_list.php',
        data: 'id=testdata',
        dataType: 'json',
        cache: false,
        success: function(result) {
            alert(result[0]);
        },
      });

PHP:

    $params = json_decode($_POST['id']);
    .
    .
    // here that i want to use data, not working an i vant get id value

    $array = array(1,2,3,4,5,6);
    echo json_encode($array);
JSON C11
  • 11,272
  • 7
  • 78
  • 65
sasan mohammadi
  • 85
  • 1
  • 2
  • 10
  • 1
    You don't need the `json_decode()` call on the server side, since the data is not sent as JSON string. The notation `dataType: 'json'` defines that the _response_ the server _sends_ will be a JSON string. The data sent to the server by that AJAX call is a plain form post. So you can simply access that parameter by `$_POST['id']` if you send the data like that instead: `data: {id: 'testdata'}` – arkascha Jan 17 '16 at 08:58
  • i tried but still not working : – sasan mohammadi Jan 17 '16 at 09:49
  • this is my jquery now: – sasan mohammadi Jan 17 '16 at 09:50
  • `$.ajax({ type: 'POST', url: 'php/pagging_employs_list.php', data: 'id=testdata', dataType: 'json', cache: false, success: function(result) { alert(result); } }); ` – sasan mohammadi Jan 17 '16 at 09:50
  • and php code is now: – sasan mohammadi Jan 17 '16 at 09:53
  • `$params = $_POST['page']; . . // here that i want to use data, not working an i vant get id value $array = array(1,2,3,4,5,6); //echo json_encode($array); echo $params;` – sasan mohammadi Jan 17 '16 at 09:54
  • Please do not post additional information in comments here. There is an `edit` button below your question. _Use it._ – arkascha Jan 17 '16 at 09:54
  • Also that ajax code you now posted clearly shows that you did not follow what I explained in my first comment. – arkascha Jan 17 '16 at 09:56
  • yes this is copy mistake i also used `data: {id: 'testdata'},` but not working. was it your mean? – sasan mohammadi Jan 17 '16 at 10:00
  • Why is it suddenly `$_POST['page']`?!? `id` and `page` are two different things. – arkascha Jan 17 '16 at 10:07
  • i'm sorry, again its copy mistake. i changed it to `id` but not working? when i remove `dataType: 'json'` from my jquery code its working but array not working! :( – sasan mohammadi Jan 17 '16 at 10:13
  • This is getting more and more unreadable. Once more: please post your code in your question, use the `edit` button for that. We need to see your current code. – arkascha Jan 17 '16 at 10:24
  • jquery code is: `$.ajax({ type: 'POST', url: 'php/pagging_employs_list.php', data: {id: 'testdata'}, dataType: 'json', cache: false, success: function(result) { alert(result); } });` and now my php code to return data is : `$params = $_POST['id']; echo $params;` it's wery simple but i dont know why with `dataType: 'json' ` not working! – sasan mohammadi Jan 17 '16 at 10:30
  • i find it .... if i remove `dataType: 'json' ` from jquery and use `var dataObj = JSON.parse(result); ` to get array from php and alert `dataObj[0])` for array working. also for sended data `data: {id: 'testdata'}` working too. but how can i send a data dynamicaly? for example i have a page number stor in `pagenum` var this code not working `data: {id: pagenum}` how can i do? – sasan mohammadi Jan 17 '16 at 10:49

1 Answers1

1

$_POST cannot be used here because it only works with url encoded data.

You need to read from stdin, for example

$data = json_decode(file_get_contents('php://stdin'));
user1983686
  • 123
  • 1
  • 8