Skip to content
Advertisement

JQuery Ajax Tagit Array to PHP returning [object Object]

I’ve tried to implement Tagit to my website, but I can’t get the array of tags to behave in a way I want it to…

I’ve googled and tried a bunch of different things but cant get it to work.

This is my JavaScript function:

JavaScript

Console.debug showed me that the “tags” array looks like this:

JavaScript

It posts to my ajaxController which has this code:

JavaScript

So, so far so good…

Then in stuff.class.php I have the function “saveSkillTags”. Here is the problem… I’ve tried to encode and decode json, and tried it just without json, but I can’t get anything to work… Here is the function:

JavaScript

As you see, I error_log $tags.

And when I run this setup I get two errors. The first one is the $tags var, and the second one is generated.

JavaScript

I’ve fiddled with this now for several hours and starting to doubt my programming skills heavily…

Please help me get back on track, even a hint would be much appreciated!

Advertisement

Answer

What you need to do is pass the data inside tags as JSON – currently it is being posted a object. In order to do this use JSON.stringify()

JavaScript

JSON.stringify is available in most modern browsers (but maybe not in some older ones). Basically it takes a Javascript object and converts it into a JSON string. Note that when stringifying, it might escape quotes and stuff, to ensure that the resultant JSON is valid.

So in the PHP script, if you do a print_r($_POST['tags']), you should see how the JSON looks and then you can adjust your code accordingly.

UPDATE

Try the following foreach loop:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement