<select> in php

Asked

Viewed 41 times

1

How do I send data via select without losing the previous ones in php? Every time I send it is overwritten =/

Just follow 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>";
    }
}

?>
  • Follow Full code<form method="POST"> <select name="test[]"> <option>Select</option> <option value="Test 1">TEST 1</option> <option value="Test 2">TEST 2</option> <option value="Teste3">TEST 3</option> <input type="Submit" value="Send"> </select> </form> <? php if(isset($_POST['test']){ $test = $_POST['test']; foreach($test as $k){ echo $k." <br>"; } } ?>

  • You can edit your question and add the code, to format select the code and hit the shortcut CTRL+K

  • It would not be the case to use the attribute multiple? ... <select name="teste[]" multiple="multiple">

  • It would be, but the client would have to hold the Ctrl p/ select more than 1 - see if you could suggest me something better friend. My idea would be : - Client select the item you want and by quantity. So far I have not found a better solution =/

  • Another option would be to use checkbox .... <input type="checkbox" name="teste[]" value="Teste1"> Teste 1

  • You can use jquery to click on the field, it inserts in a form a list item already with the name you clicked and in front a field to place the quantity, so every click it creates a new item, at the end of this form you leave the send button

  • I’ll make an example here, just a moment

Show 2 more comments

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>

        <meta charset="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>

        <!-- Include all compiled plugins (below), or include individual files as needed -->
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

    </body>
</html>

To request the php file I recommend to use ajax, it is very easy, just take a look at examples of jquery. But that’s it, now just recover the values and make the request with ajax that is all quiet, I hope to have helped

  • Friend did not know that Ajax is so powerful p/ this kind of operation (confess and my knowledge in Ajax and Jquery is almost 0). From now on more than grateful brother. I will delve into them. Quarrel!!!

  • @Maxwellgomes and if I tell you that I started learning ajax yesterday kkkkkkkkk, my knowledge was totally 0 too, but I saw that it is quite easy until, I’m glad that solved your problem there

  • Our guy! So congratulations ae rsrs... I’ll start today, I bought an EAD course, I’ll start today. Any course suggestions?

  • @Maxwellgomes I’m doing this: https://www.udemy.com/curso-completo-development-web/learn/v4/ found it very good

  • Thanks brother !

Browser other questions tagged

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