1

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

4
  • send variable to a php page or use ajax Commented Aug 2, 2013 at 12:55
  • i can prepare php page for saving info in db, but how can i send my js object to it as you proposed? thanks Commented Aug 2, 2013 at 12:56
  • @HoussemBdr Execute a HTTP request with JS (AJAX), posting the JSON string along with it. On your target (PHP) page you will have the JSON string in the $_POST[] or $_GET[] super global. Commented Aug 2, 2013 at 13:11
  • @AlexP I am working on it Commented Aug 2, 2013 at 13:39

1 Answer 1

3

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";
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry but you did not understand what i asked for

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.