Javascript Object to a PHP Object

optikalefx · 27K views · 2010 · 8:38

Open on YouTube

Web developmentPython, Ruby & scripting languagestutorialdated

Why Stack Overflow recommends it

Stack Overflow users link it when answering how to pass a JavaScript object to PHP and decode it on the server, even though some find the old code needs fixing.

Recommended by 1 people across 1 different months, 2011 to 2021.

What it is

A short 2010 screencast by optikalefx showing a custom JavaScript function that serializes a JS object to a JSON string, posts it, and decodes it in PHP with json_decode. It covers stripping the slashes added during the POST and preserving indexed arrays.

What people said

Recommends user743234 · 2011-06 · Stack Overflow · on “passing PHP objects to javascript”

You can convert the PHP object to an array, and then use JSON function to encode it. After that, decode it from JavaScript. Here are some basic steps: Convert [LINK] the PHP object to an array. Using json_encode() [LINK] to encode that PHP array. Pass [LINK] JSON-encoded data to PHP Decode JSON from JavaScript using JSON.parse [LINK] or you can use jQuery.parseJSON [LINK] to do that. This is an interesting tutorial about passing a JavaScript object to a PHP object [this video] . You might find it useful by watching some other featured/related videos. Hope this helps.

Cites adamj · 2015-12 · Stack Overflow · on “how to convert a JavaScript literal object into Json object in PHP”

JS: // Pretend we're POSTing this var foo = {foo:{first:"George",middle:"William"}}; PHP: $foo = $_POST['foo']; $foo = json_decode( stripslashes( $foo ) ); echo $foo->first; Credit where credit is due: [this video]

Cites Edward · 2021-02 · Stack Overflow · on “objects, php, javascript errors”

Javascript Object to a PHP Object [this video] I was searching for the tutorial how to pass associative array from JavaScript to PHP object and found this video. I think it is a little bit old it was added in 2010. I tried it and had error. I ask you to help me to correct it or help me to to get php object from js object. index.php <script> let ar = new Array(); ar["name"] = "name1"; ar["name1"] = "name2"; console.log(ar); function convertOb(object){ var json = "{"; for (property in object) { var value = object[property]; if (typeof(value) == "string") { json += '"' + property + '":"' + value +'",'; } else{ if (!value[0]) { json += '"' + property + '":"' + convertOb(value) + ','; } else{ json+= '