How to pass an object from php 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
1. Code:
$this->title,
'content'=>$this->content
);
}
}
$myExampleInstance= new MyExample();
$myExample = json_encode($myExampleInstance); // {"title":"Convert object php to JS","content":"this example will you convert an object php to object javascript"}
echo $myExampleInstance;
?>
2. Basic steps:
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 the PHP object to an array.
- Using json_encode() to encode that PHP array.
- Pass JSON-encoded data to PHP
- Decode JSON from JavaScript using JSON.parse or you can use jQuery.parseJSON to do that.