How to enter several data received from the same field?

1

I have a form in html that has a field where the user inserts the data as shown in the code below, this form is dynamic and the user can insert as much information as he wants, in the following case I put only six systems. / p>

My question is: How will I receive this amount of information in PHP by $ _POST and insert into the bank? The query I even know how to do, but I know I have to work with a repeat structure, but how?

HTML code:

<div id="container">
<ul class="tags">
<li class="addedTag">
sistema1
<span class="tagRemove" onclick="$(this).parent().remove();">x</span>
<input type="hidden" value="sistema1" name="tags[]"/>
</li>
<li class="addedTag">
sistema2
<span class="tagRemove" onclick="$(this).parent().remove();">x</span>
<input type="hidden" value="sistema2" name="tags[]"/>
</li>
<li class="addedTag">
sistema3
<span class="tagRemove" onclick="$(this).parent().remove();">x</span>
<input type="hidden" value="sistema3" name="tags[]"/>
</li>
<li class="addedTag">
sistema4
<span class="tagRemove" onclick="$(this).parent().remove();">x</span>
<input type="hidden" value="sistema4" name="tags[]"/>
</li>
<li class="addedTag">
sistema5
<span class="tagRemove" onclick="$(this).parent().remove();">x</span>
<input type="hidden" value="sistema5" name="tags[]"/>
</li>
<li class="addedTag">
sistema6
<span class="tagRemove" onclick="$(this).parent().remove();">x</span>
<input type="hidden" value="sistema6" name="tags[]"/>
</li>
<li class="tagAdd taglist">
<input type="text" id="search-field"/>
</li>
</ul>
</div>

PHP:

<?php 

    $tags = $_POST['tags']; 
    $query = mysqli_query($dbc, ""INSERT INTO tb_conhecimentos .......
?>
    
asked by anonymous 11.10.2016 / 01:42

1 answer

2

This form will return something like this:

Code:

<?php

    var_dump($_POST['tags']);

Output:

array(6) { [0]=> string(8) "sistema1" 
           [1]=> string(8) "sistema2" 
           [2]=> string(8) "sistema3" 
           [3]=> string(8) "sistema4" 
           [4]=> string(8) "sistema5" 
           [5]=> string(8) "sistema6" }

How do you record these items?

With foreach :

<?php

    foreach($_POST['tags'] as $value):
        echo $value;
    endforeach;

$value would be each item sent within that array of tags .

If the version of PHP is >= 5.2.0 can use filter_input :

<?php

    $tags = filter_input (INPUT_POST, "tags", FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
    foreach($tags as $value):
        echo $value;
    endforeach;
    
11.10.2016 / 02:10