How to mount an array with bootstrap tagsinput values?

Asked

Viewed 366 times

1

I’m using a bootstrap tagsinput to insert values in the bd, but the values are being stored in a single string, for example: "256,257,299". I would like these values to be set in an array and look like this: ["256", "257", "299"], ai dou um foreach e cada valor vai para sua devido lugar no bd. As of today, all in a single string only the first id is being sent to the bd.

Does anyone know how to perform this procedure?

This is my input:

<input type="text" id="taginput" data-role="tagsinput" name="idrelacionado[]"> 

Grateful for the attention!

  • Have you thought about using the split()?

  • The split is not obsolete? , The bootstrap also indicates to use this way: <select Multiple data-role="tagsinput" name="idrelacionado[]></select>. I’ve tested and met my need, but I don’t know if it’s the right way.

  • why the split would that be obsolete? The bootstrap indicates to do so, but does not deliver in an array format, but rather an easier format for you to transform the value into an array using the split

  • In php.net it says that this function is obsolete in version 5.3.0 and has been removed from version 7. Here are other alternatives to use.

  • Ah, I thought we were talking about Javascript. I can’t help but since the documentation says it’s obsolete, see what alternative it gives you.

  • Funny that on the tagsinput page - Bootstrap says that using this select Multiple the values will be set in an array, as you can see: Use the <select Multiple /> as your input element for a tags input, to Gain true multivalue support. Instead of a comma separated string, the values will be set in an array. Existing <option /> Elements will Automatically be set the tags. This makes it also possible to create tags containing a comma.

Show 1 more comment

3 answers

2


The way I solved the problem:

In my form I have the following input (tagsinput):

<input type="text" data-role="tagsinput" name="idrelacionado"> 

I receive this string in the following variable:

$idrelacionado = $_POST['idrelacionado'];

Aplico the explode in the variable that stores the string, where the delimiter is the comma "," and stored in another variable.

$idre = explode(',', $idrelacionado);

Then just give a foreach on the variable that stores the explode.

  • This would be my answer, how nice that you arrived at the solution alone. Congratulations!

  • Thank you @Ivanferrer.

0

Hello I had the same problem and the solution was none of the presented here and nor searching in google resolvi rsrs, I was almost using another script ai when it was implementing saw that it uses the field select to the inves of input ai I thought po ai the solution, just put the field as select aidionei the items in js and put the pads in the field name and it worked, so let’s go the step by step..

Javascript

tagsinput('items');

HTML

<div class="form-group">
<label>Campo Array</label><br>
<select name="NomeCampoArray[]" data-role="tagsinput" multiple="multiple" multiple data-placeholder="Digite as tags separadas..." value="<?php print_r($NomeCampoArray);?>">
<optgroup label="NomeCampoArray">
</select>
</div>

0

Okay, what a Google won’t solve.

"Php split deprecated what to use Instead" https://stackoverflow.com/questions/2813241/php-split-alternative

UPDATE

Okay, dude, another Google and I found your answer, link

Like every jQuery component, you can do "magic" with Javascript.

As you can see, already on the first page, already has what you need.

$("input").tagsinput('items')

Calling the method items of its component tagsinput te returns the values in an array.

  • Yes there are PHP alternatives I haven’t tried to use them yet because I was wondering if I could get to a solution only with Bootstrap, because on the page are presented ways to use the tool that seemed to me maybe there is an option how to solve the problem. And as I informed you also had already visualized the alternatives of the split.

  • But thank you so much for the guidance, I will be studying for future if necessary to implement. It seems to me that str_split is a good option.

  • Dear Gustavo, the way you address the answer to understand as if I did not search before asking the question in the forum, I tried to use jquery to solve this problem but I could not or could not implement. Since I’m a beginner sometimes I may lack nomenclatures for Google search, linking unsatisfactory results.

  • In the script: $("input"). tagsinput('items') in place of the input should I put my input id ? This script goes on in the head ?

  • That, but in case it’s an id, you should put a # in front of input id, no space, pasted with id

  • um... this script can be inserted into the body of html tbm, before input ? and just call inside the script tag ? Worst guy I ever tried to use it but it didn’t even work pal, I’ll try again, who knows the right.

  • You have to use this in a script tag, after all your HTML, before closing the body. Don’t get me wrong, but after this question I suggest you take a look at web technologies, HTML, jQuery Javascript. Try to sign Alura.com.br , is very worth the investment.

  • Because "after this question", as I informed you I am beginner compared to the great knowledge of others, really in the part of "tags scripts" I asked I do not know why, because script has to be within the tags script, But nothing I need to go into studying html and web technologies, because sometimes we eat ball and it’s not because of lack of knowledge, but because of how to express yourself, maybe speaking personally it would not sound so lacking of knowledge. But anyway, thanks for the school tip.

  • Yeah, the way you asked about using script before and out of script tag gave a fright. The importance of using the script last is due to the fact that if it is imported before, it will run and when it runs your element with taginputs id still does not exist, then it will not work and you will suffer a little until you understand the reason.

  • Valew Gustavo, I will be implemented this script to my application. If successful, I will be posting the code here. is nozis!

  • Blz, anything looks in Hangouts

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.