PHP JS

SIMPLE JSON EXAMPLE WITH PHP JAVASCRIPT

PHP ENCODE - ARRAY TO STRING  $data = json_encode(["Red", "Blue"]); PHP DECODE - STRING TO ARRAY $data = json_decode($data, 1);

BASIC JSON IN PHP & JS

01

JS ENCODE - ARRAY TO STRING var data = JSON.stringify(["Red", "Blue"]);  JS DECODE - STRING TO ARRAY data = JSON.parse(data);

JSON ENCODE ARRAY TO STRING var data = new FormData(); data.append("demo", JSON.stringify(["Red", "Blue"]);

SEND ARRAY IN JS

02

SEND FORM DATA TO SERVER fetch("SERVER.PHP", { method:"POST", body:data });

ENCODE - ARRAY TO STRING $data = json_encode(["Red", "Blue"]);

SEND ARRAY IN PHP

03

PHP CURL SEND $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://site.com"); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_exec($ch); curl_close($ch);