select in php

1

How do I send data by select without losing the previous ones in php? Every time I send the same is replaced = /

Here is my code:

Ex:

<form method="POST">
<select name="teste[]">
    <option>Selecionar</option>
    <option value="Teste1">TESTE 1</option>
    <option value="Teste2">TESTE 2</option>
    <option value="Teste3">TESTE 3</option>

<input type="submit" value="Enviar">

</select>
</form>
<?php
if(isset($_POST['teste'])){

    $teste = $_POST['teste'];

    foreach($teste as $k){
        echo $k."<br>";
    }
}

?>
    
asked by anonymous 28.03.2018 / 04:14

1 answer

0

Take a look, I used bootstrap and jquery:

    <!DOCTYPE HTML>
    <html lang="pt-br">
    <head>
        <meta charset="UTF-8">

        <title>Twitter clone</title>

        <!-- jquery - link cdn -->
        <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script><metacharset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <link rel="icon" href="imagens/icone.png">

        <!-- Bootstrap -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

        <script type="text/javascript">
            $(document).ready( function(){

                $("#add").click(function() {
                    var v = '<a href="#" class="list-group-item"><strong>' + $("#v1 option:selected").val() + '</strong>  -  <input type="text" name="valor" placeholder"Digite o valor"></a>';
                    $('#form_pro').append(v);

                }); 
            });

            //Apos adicionar tudo que você quis, vc faz a requisição ajax para seu arquivo php

        </script>

    </head>

    <body>

        <div class="container">

            <div class="col-md-4">
                <select name="teste" id="v1" class="form-control">
                    <option>Selecionar</option>
                    <option value="Teste1">TESTE 1</option>
                    <option value="Teste2">TESTE 2</option>
                    <option value="Teste3">TESTE 3</option>

                </select>

                <button id='add'>Adicionar</button>

            </div>

            <div class="col-md-8">
                <div class="panel panel-default">

                    <div class="panel-body">
                        <form id="form_pro" class="list-group">
                            <!--Dados são inseridos aqui-->

                        </form>
                        <button>Requisitar</button>
                    </div>
                </div>
            </div>

        </div>


        </div>

        <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script><!--Includeallcompiledplugins(below),orincludeindividualfilesasneeded--><scriptsrc="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

    </body>
</html>

To make the request for the php file I recommend that you use ajax, it's pretty easy, just take a look at jquery examples. But it is not anymore, now only recover the values and make the request with ajax that everything is quiet, I hope I have helped

    
28.03.2018 / 06:25