I am developing a system to manage products and publish products from CMS but I want to give this final touch to the CMS system a simple tag system.
This is a simple tag system developed with jQuery, each word separated by a comma becomes a tag. Examples: php, jquery, etc.
If I add two identical words, they are added without problem. How to avoid duplicate tags?
Those tags generate this html code.
<span>php</span>
<span>jquery</span>
I want to control which tags can be added, for this it is necessary that this simple system requires a database where it is stored which tags can only be selected.
Example
id tags
1 php
2 jQuery
To thus avoid labels for example hello, ajdjj, etc.
Formulating a question in SO in English on how to rescue the values of the labels to insert the data into the database, they gave me this solution:
var tags = '';
$('#tags > span').each(function() {
tags = tags + $(this).html() + ',';
});
$('#inputInForm').val(tags);
Perhaps this question goes off topic SO.
Despite having knowledge of php and mysqli, I find it difficult to mix programming with jQuery. But I'm rude in mixing php and mysqli with jQuery, I don't know how to use it. Can you help me with a simple example to complete this basic tag system?
I don't need a complete system, I just need a small example of how to use it.
$(function(){ // DOM ready
// ::: TAGS BOX
$("#tags input").on({
focusout : function() {
var txt = this.value.replace(/[^a-z0-9\+\-\.\#]/ig,''); // allowed characters
if(txt) $("<span/>", {text:txt.toLowerCase(), insertBefore:this});
this.value = "";
},
keyup : function(ev) {
// if: comma|enter (delimit more keyCodes with | pipe)
if(/(188|13)/.test(ev.which)) $(this).focusout();
}
});
$('#tags').on('click', 'span', function() {
$(this).remove();
});
});
#tags{
float:left;
border:1px solid #ccc;
padding:5px;
font-family:Arial;
}
#tags > span{
cursor:pointer;
display:block;
float:left;
color:#fff;
background:#789;
padding:5px;
padding-right:25px;
margin:4px;
}
#tags > span:hover{
opacity:0.7;
}
#tags > span:after{
position:absolute;
content:"×";
border:1px solid;
padding:2px 5px;
margin-left:3px;
font-size:11px;
}
#tags > input{
background:#eee;
border:0;
margin:4px;
padding:7px;
width:auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="tags">
<span>jquery</span>
<input type="text" value="" placeholder="Add a tag" />
</div>
For your case, the following occurred to me, first to have a function that will be in charge of saving the tags added by the user in a global array.
}
The function called "add_tags" receives as parameters: tag (the tag written by the user that you want to enter) and arrtags (global array that stores the tags written by the user). This function is responsible for saving the tags written by the user in the global array without repeating them. Thanks to this function we prevent the user from entering repeated tags. I adapted the previous function from the following example: Unique array in javascript
Well, as we can see, within the "add_tags" function we call another call "control_tags", this is responsible for adding only the tags stored in the database, comparing if the tag is within the array that stores the tags from the database. database.
}
control_tags returns true if the tag entered by the user is in the array of tags from the database, otherwise it returns false if the tag is not one of those stored in the database. With this we prevent users from entering tags that are not allowed. The control_tags function is very useful because it is like a PHP in_array but adapted to Javascript.
Therefore, two global arrays must be created, one that already saves the tags from the database and another array to save the tags added by the user. The array that stores the database tags would look like this:
And your tag code would look like this:
});
How to load the tags from the database in the arr_db array? It occurs to me through ajax, on the PHP side you make the corresponding query, you return a json, and in javascript you go through it with a for and with arr_db.push(returnedData[i]) you save the tags in the array.
Basic example of database query in PHP and json return with all saved tags:
Example with ajax to load the tags from the database in the global array:
Finally in the next block you must add the following:
remove_tags is a function that is responsible for removing the tags from the global array, which saves the tags selected by the user, when the user performs the action of removing a tag. Source : Removing elements from an array
I hope this guide will help you and everyone who goes through something similar. Cheers!
I would do something like this in JS:
And the PHP would do something like this:
First you load the content of an array with PHP, this content can be extracted from a database with MySql
So now you have to query if the entered word exists in the array loaded from PHP if(jQuery.inArray(txt, arr_php) !== -1)
In your code you replace:
by:
There are many plugins for jQuery to add tags to forms.
jQuery autocomplete tagging plugin like StackOverflow's input tags?
I don't know if you are using any framework, but I know some that allow you to add content to certain "zones" in the form of blocks. Twig for example allows you to do something like:
I don't remember if this is the syntax.
The point is that in those blocks you could either include your array of allowed tags by declaring a variable in JS, or implement a helper that prints the plugin implementation completely, just telling it maybe the field name, allowed tags, etc.
You can serialize your object to a JSON object that you can consume from JavaScript:
Wherever the tag array comes from, whether it's from the database or wherever, you can serialize the array to JSON and put the content of the variable
$allowedTags
(a JSON object) into a JavaScript variable.You have to be especially careful with Latin characters, for example. Also correctly handle the quotes of the keys of the JSON object.
json_encode