Skip to content
Advertisement

Insert select2 tags jQuery array in PHP

I have seen many threads on stackoverflow about this concern but I couldn’t achieve that so that made this post again. I am using select2 jQuery plugin where I want to insert Tags in SQL using PHP. Like [Data, Data, Data]

I try to do that which I learn through google, well I am professionalize in this, I am new in PHP. Please help ‘How I Insert this in DB where show , between two words as above I mentioned in Like

My Code Is

JavaScript
JavaScript

Advertisement

Answer

Your form is likely being sent like this:

JavaScript

So, since you are wanting it to be formatted as brown, Green, Blue, then you can explode() and implode() or just use a str_replace():

JavaScript

If you are trying to split up that string to insert each tag separately, you would use explode() on the comma, then loop the results of the explode().

I would probably use trim() in there to remove empty space, just incase. Also, if you want to make sure they are all formatted the same, you might want to do ucwords() to make sure each word has an uppercase as the first letter (your default value brown is all lower and the reset have the first letters upper).

If you do method 1, you can apply a ucfirst() and trim() if you use array_map() on the exploded string:

JavaScript

would give you the string:

JavaScript

EDIT:

Since you are actually storing per row, you can fetch back to an array using a select:

JavaScript

To use:

JavaScript

EDIT #2

To do it both together on the same page, you would just use json_encode():

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