1atera1usz
1atera1usz

Reputation: 143

json_decode in PHP returns null for valid JSON

I am submitting form from jquery like this:

                    var submitForm = $('<form></form>').attr("action", "proceed.php").attr("method", "post");
                    var input = $('<input>').attr("type", "hidden").attr("name", "jsondata");//.attr("value", "");
                    var data = [];
                    elmProductSelected.children("option").each(function(n) {
                        var pIdx = $(this).attr("rel"); 
                        var pObj = products[pIdx]; 

                        var pname = pObj.manufacturer + " " + pObj.name;
                        var pdesc = pObj.description;
                        var pprice = pObj.price;
                        var pimg = pObj.img;

                        var jsonProd = new Object();
                        jsonProd.pname = pname;
                        jsonProd.pdesc = pdesc;
                        jsonProd.pprice = pprice;
                        jsonProd.pimg = pimg;

                        data.push(jsonProd);
                    });                 
                    input.attr("value",data);

                    submitForm.append(input);
                    submitForm.appendTo(document.body).submit();

On proceed.php I'm trying to get this JSON like this:

if(isset($_POST["jsondata"])) {
    echo "data received :   ";
    $data = json_decode($_POST["jsondata"], true);
    var_dump($data); 
} else {
  echo 'no data received';
  die();
}

What I get from server is:

data received : NULL

My JSON is valid, tested it with http://jsonlint.com/ and it looks like this:

[
{
    "pname": "Yamaha AES1500 ELECTRIC GUITAR ORANGE STAIN / CASE",
    "pdesc": "AES1500 combines the classic look and tone from the golden era of full-bodied rock with the modern advantages of precision engineering. Offered with the option of the famous Bigsby vibrato tailpiece, the AES1500 combines the character of classic American archtop-electric design with Yamaha high quality construction. The semi-thinline AES1500, a classy, comfortable archtop cutaway design, is an instrument that successfully straddles the rock, country and jazz genres. The two custom Q100 DiMarzio humbuckers possess a powerful warmth perfectly balanced by the clear, well defined cut-through of the maple body construction. Moreover, each pickup can be coil split, which in conjunction with the three-way pickup selector lends a whole new dimension of tonal versatility and subtlety.",
    "pprice": 207101.43,
    "pimg": "5b2f8d703c899290cbcbe325058dfaeb"
},
{
    "pname": "Yamaha AES1500 ELECTRIC GUITAR RED STAIN / CASE",
    "pdesc": "AES1500 combines the classic look and tone from the golden era of full-bodied rock with the modern advantages of precision engineering. Offered with the option of the famous Bigsby vibrato tailpiece, the AES1500 combines the character of classic American archtop-electric design with Yamaha high quality construction. The semi-thinline AES1500, a classy, comfortable archtop cutaway design, is an instrument that successfully straddles the rock, country and jazz genres. The two custom Q100 DiMarzio humbuckers possess a powerful warmth perfectly balanced by the clear, well defined cut-through of the maple body construction. Moreover, each pickup can be coil split, which in conjunction with the three-way pickup selector lends a whole new dimension of tonal versatility and subtlety.",
    "pprice": 207101.43,
    "pimg": "5b2f8d703c899290cbcbe325058dfaeb"
}
]

If I, instead of this data, send something like:

var a ="{\"id\":\"1\"}";                        
input.attr("value",a);

server successfully gets json:

data received : array(1) { ["id"]=> string(1) "1" }

Question: What could possibly be wrong with first request?

Thanks.

Upvotes: 0

Views: 148

Answers (1)

Kevin B
Kevin B

Reputation: 95022

input.attr("value",data); sets the default value, not the current value. To set the current value, use .val().

Additionally, the true cause of the problem is what you have is an object, not json. You should convert it to json if you want to send it as json, otherwise you will in fact send invalid json [object Object, object Object]

input.val(JSON.stringify(data));

Upvotes: 2

Related Questions