Take this jsfiddle demo example , how to reference and guide to add effects and show error message.
Achieve the same effect, but the error message validation does not, when inserting a duplicate tag or a disallowed tag it shows the same error message "Cannot add duplicate tag" now when deleting the tag the error message remains not gone .
Full code running:
https://jsfiddle.net/39m2nukg/
//var arr_db = [];
var arr_db = ["html", "css", "jquery", "javascript", "php"];
var usuario_tags = [];
$(function(){
$.ajax({
url: 'tags.php',
success: function(result) {
var returnedData = JSON.parse(result);
var total = returnedData.length;
for(var i = 0; i < total; i++) {
arr_db.push(returnedData[i]);
}
}
});
// ::: TAGS BOX
$("#tags input").on({
focusout : function() {
var txt = this.value.replace(/[^a-z0-9\+\-\.\#]/ig,''); // allowed characters
var control = agregar_tags(txt.toLowerCase(), usuario_tags);
var verifica_tags = control_tags(txt.toLowerCase(), arr_db);
if(txt && control == false && verifica_tags == true) {
$('#message').hide();
$("<span/>",
{
text: txt.toLowerCase(),
insertBefore:this
});
} else {
$('#message').show();
}
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();
quitar_tags(usuario_tags, $(this).text());
});
});
function agregar_tags(tag, arrtags) {
var index = -1;
var resultado = control_tags(tag, arrtags);
for(var i = 0; i < arrtags.length; i++) {
if(arrtags[i] === tag) {
index = i;
}
}
if(index > -1) {
arrtags[index] = tag;
} else {
arrtags.push(tag);
}
return resultado;
}
function control_tags(needle, haystack) {
var length = haystack.length;
for(var i = 0; i < length; i++) {
if(typeof haystack[i] == 'object') {
if(arrayCompare(haystack[i], needle)) return true;
} else {
if(haystack[i] == needle) return true;
}
}
return false;
}
function quitar_tags(array, element) {
const index = array.indexOf(element);
array.splice(index, 1);
}
#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>
<form method="post">
<label>Título<br/>
<input type="text" name="titulo" maxlength="255" />
</label><br/>
<label>Noticia<br/>
<textarea name="novedad"></textarea>
</label><br/><br/>
<div id="tags">
<input type="text" value="" placeholder="Add a tag" />
</div>
<p id="message" style="display:none">No se puede añadir una etiqueta duplicada.</p><br/><br/><br/>
<button type="submit">Publicar noticia</button>
</form>
I am misapplying the parameters or variables in the code jQuery is very different from php, could you guide me what I should take into account to correctly apply these error messages.
How do I correctly apply these error messages to jQuery code?
Example:
Show error message when adding a duplicate tag.
Show error message when trying to add a not allowed tag, the allowed tags are taken from this direct variable or from a database
var arr_db = [];
Limit the maximum of labels and at the same time show an error message.
Doe.
First of all I invite you to use a clearer code with more descriptive variables to make it more readable, simple and effective. In this way, you will not only know more easily where the errors are, but also have better control of the code, no matter how large it is. Starting to use a style guide could be a good step.
As far as what you're trying to do, it seems to me that the code could be made even simpler. A basis for this could be the following working example:
NOTE: The only thing you would have to do to finish it would be to add the query, addition and removal of these tags via ajax in their respective methods.
I leave the example made without using jQuery , it is not as elegant as the one by @MauroAguilarBustamante but it fulfills the function requested.
I put the prototypes of the functions that I just programmed.
function esDuplicado(texto)
function esEtiquetaValida(texto,contiene)
function borrar(texto)
generarBaseDatos()
function agregarEtiqueta()
Also, status messages are added correctly.
"Etiqueta duplicada."
"Listo."
"Escribiendo..."
"Etiqueta no válida."
End code:
PHP: This is how you save it in a single record:
JavaScript: try the following code (only Js has been modified), I have already modeled this to follow the object-oriented programming (OOP) paradigm, the documentation is already included in the code:
Considering that you can't send an array as such by post to php I thought of transforming it to json or separating the keywords by commas, using the last option the PHP should also be modified and use an explode instead of an implode to separate the array the commas, and with a foreach in php :
FULL PHP CODE:
FULL HTML CODE:
This resolves to send data to php to be saved in a database.