Can't process json array from Ajax in php

After a lot of searches in Stackoverflow and elsewhere I can't get my array of Json objects in php.

my javascript file : src.js

var jq = jQuery.noConflict();
var jarray = [{"key":0,"keysdata":1},{"key":1,"keysdata":2}];
//json objects-array

function doAjaxRequest(jarray){
jq.ajax({
url: "test.php",
type: "post",
data: {out : JSON.stringify(jarray)},
success: function(response) {
alert(response);
alert("Ajax Transmitted successfully");
}           
});  
}   

My php file :test.php

<!doctype html>
<html>
<head><title>In php from ajax</title></head>
<body>
<?php
if (isset($_POST['out'])) {
$objs = json_decode($_POST['out']);
echo 'K0=' . $objs[0]->key . ', data0=' . $objs[0]->keysdata;
}
else{
echo 'out not posted';
}
?>
</body></html>

Every thing works fine upto here. My alert(response) in the success function shows

<!doctype html>
<html>
<head>
<title>In php from ajax</title>
</head>
<body>
k0=0,  data0=1</body>
</html>

But if I access my test.php in the browser's (chrome) url box as localhost/test.php then I find the output as

out not posted

Can anybody please explain what's going on here? My intention is to carryout some processing on the array elements in the PHP side and to create a form dynamically using php (in the page test.php) to help user edit the data values (viz., 1 and 2) and the browser url would appear as localhost/test.php. I even can't find the size of the array $objs by writing $size = count($objs); var-dump($objs) shows Array (size=0).

Waiting to appreciate your proper inputs on this issue.


It's because when you access a page in the browser the request is sent through a GET HTTP verb.

Your PHP code is set to only detect data sent via the POST verb - as witnessed by your use of $_POST['out'] - hence execution of the script follows the alternate logical path where you only echo out not posted to the output.


To test it directly from the php page, you need to pass it the data, because you're passing an array of objects, it would be pretty messy to do that via the url. Try this in php file.

http://localhost/test.php?testmode

<!doctype html>
<html>
<head><title>In php from ajax</title></head>
<body>
<?php
if (isset($_POST['out'])) {
    $objs = json_decode($_POST['out']);
    echo 'K0=' . $objs[0]->key . ', data0=' . $objs[0]->keysdata;
} elseif(isset($_GET['testmode'])) {
      $str = '[{"key":0,"keysdata":1},{"key":1,"keysdata":2}]';
      $objs = json_decode($str);
      echo 'K0=' . $objs[0]->key . ', data0=' . $objs[0]->keysdata;
} else {
    echo 'out not posted';
}
?>
</body></html>

To create an array of objects in PHP do the following:

$myArray = array((object)array("key" => 0,"keysdata" => 1),(object)array("key" => 1, "keysdata" => 2));

Friends, this is with reference to my questions posted here on 14 and 16th May 2017. The questions are respectively: 1. Array of JSON objects successfully sent to php using jquery ajax but php reports array size=0

  • Can't process json array from Ajax in php
  • I have been working on the issue since 12th May 2017 and in a week's time thankfully after thoroughly introspecting my codes etc. I got the problem and the solution as well. I though I should share the story with people, so anybody in a situation like mine can help themselves.

    When I run my page containing jquery, everything works fine. But if I access the target php manually, I have been constantly getting the unknown index "out" in my target php file ......... on line .....

    I had been wondering why PHP not getting the value of "out" despite chrome debugging and all alerts in JQuery file working perfectly to reveal that the data transmission to and from PHP is OK.

    After a lot of reviews, I finally could discover that PHP requires "out" to be posted from a form via an element with the name "out".

    I then revised my code in the Jquery page to store the Json array "jarrary" after stringify-ing to a hidden textbox element with the name "out" (using jquery coding). Then I made the form post through Jquery submit() method. And the things worked ! Now if I manually access the PHP page I get that both the PHP side and JQuery side are working fine.

    链接地址: http://www.djcxy.com/p/58128.html

    上一篇: Symfony:为什么\ DateTime()中的反斜杠;

    下一篇: 无法在php中处理来自Ajax的json数组