- JSON.stringify(obj);
-
If value includes double quote or single quote, and need to be passed from php to javascript:
$banners = preg_replace("/\\\'/", ''', $banners);
$banners = preg_replace('/\\\"/', '"', $banners); -
Put JSON string in HTML tag attributes: Double quote must be converted to "
e.g. $version = str_replace('"', '"', $version);
Summary steps:
- JS send to PHP: var json = '{"id":1,"title":JSON.stringify(title)}';
- Save to DB in PHP: Don't need to use json_encode()
- Retrieve by JS: $.parseJSON(json);
- Retrieve by PHP: json_decode($json);