I have a normal div, having an input textbox which can be edited manually. I want to add/remove php values from another div having a set of php array values (from a query). Each value have an [Add] or a [Remove] button that is based on weather the corresponding php value exists in the input text field or not.
So, basically if the textbox is empty, then all the buttons will be [Add] and clicking it will add the corresponding php value to the textbox, and then change into a [Remove] button. Likewise, any [Remove] button will remove the corresponding php value from the textbox, and then change into an [Add] button.
EXAMPLE:
$values = array("Mike", "James", "Jerry", "Tom");
input type="text"
If I manually made any changes to the textbox or entered some known php values into it (separated by a semi-colon):
James;Mike;Hyde;Jerry;
The other div will dynamically search for all the values of the php array and add a [Remove] button for the ones that exist, and an [Add] button for those who were not found in the textbox.
James [Remove]
Mike [Remove]
Tom [Add]
Jerry [Remove]
Notice #1: Here, 'Tom' is the only value that do not exist in the textbox, so it has a [Remove] button.
Notice #2: Also, 'Hyde' is not known as a value of the php array, so it has neither an [Add] nor a [Remove] button.
Edit:
What I already did, is basically echo all array values from the php query and attach each with an [Add] button (since the textbox is empty by default)
I'm using custom js functions similer to:
$("#textbox").val(function(i, val) { return val.replace(phparrayvalue, ''); });
But, in order to dynamically change the buttons between [Add] and [Remove], I'll have to keep the php array all the time (AJAX requests will be slow to get the php values ontextchange).
I think the problem would vanish if I could pass and convert the php array into a javascript one onload. After that it would be easy to compare the saved js array with the textbox value ontextchange, and change buttons based on the result.
I hope it is clear and someone can help with the javascript code.
Thank you