I have a javascript object (with stringify, i can make it in json format), like that:
var jsObject ={'elem1':'val1', 'elem2': {'elem21':'val1', 'elem22': 'val2'}};
I want to save it in my already prepared database, and that using php of course. My problem, is how can i communicate with that js object so that i can save its elements into my db using php? for example, i my use json_encode()
any help
1 Answer 1
You can use the serialize function ported from php in php.js: http://phpjs.org/functions/serialize/ or you can te parseJSON function by jquery http://api.jquery.com/jQuery.parseJSON/ or the javascript native object JSON.stringify(obj);
Using jquery:
$.post('file.php', {
data_to_save: JSON.stringify(obj)
}, function (r) {
alert(r)
});
in php
$data_from_js = $_POST['data_to_save']; //It's a string contains the "jsonizzed" javascript object
//Do the query to save string from $data_from_js to database
echo "success";
$_POST[]or$_GET[]super global.