How to take the value of a select and fill in an input?

Asked

Viewed 2,945 times

2

inserir a descrição da imagem aqui

 <?php
    $con=mysql_connect("localhost","root","") or die("Failed to connect with database!!!!");

    mysql_select_db("bancoteste", $con); 
    $sth = mysql_query("SELECT * FROM pessoa");

?>



<!DOCTYPE html>
<html>
    <head>
        <title>bselect</title>
        <meta charset="UTF-8" />

        <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.2/css/bootstrap.min.css" />
        <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.2/css/bootstrap-responsive.min.css" />
        <link rel="stylesheet/less" href="src/bselect.less" />
        <link rel="stylesheet" href="support/styles/prettify.css" />

        <!--[if lte IE 8]>
        <script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.6.2/html5shiv.js"></script>
        <![endif]-->

        <script src="//cdnjs.cloudflare.com/ajax/libs/less.js/1.3.3/less.min.js"></script>
        <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <script src="src/bselect.js"></script>
        <script src="support/scripts/prettify.js"></script>
        <script src="support/scripts/example.js"></script>
        <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

    </head>
    <body>
        <div class="container">

                <br>

                <div class="span6">
                    <label for="bselect-standard">PHP:</label>

                    <select class="languages span" id="bselect-standar">
                        <option value="">Selecione</option>

                        <?php
                            while($r = mysql_fetch_assoc($sth)) {
                                    echo "<option value='".$r['idpessoa']."'>".$r['nomePessoa']."</option>";
                                }
                        ?> 
                    </select>
                </div>


            </div>



            <!-- Modal -->
            <div id="myModal" class="modal fade" role="dialog">
              <div class="modal-dialog">

                <form action="enviar.php" method="post">

                    <!-- Modal content-->
                    <div class="modal-content">
                      <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                        <h4 class="modal-title">Cadastrar Nova Entrada</h4>
                      </div>
                      <div class="modal-body">
                        <p>Nome Pessoa</p>
                        <input type="text" name="nomePessoa" id="nomePessoa" value="">
                      </div>
                      <div class="modal-footer">
                        <button type="submit" class="btn btn-success" >Save</button>
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                      </div>
                    </div>

                </form>


              </div>
            </div>

    </body>
</html>
  • but Voce wants the modal to appear when Voce selcionce, or Voce will put a button to send the select?

  • I want to take the name of the select and fill in the imput field in the modal. Modal already opens but has nothing in the field of imput

  • I tried with Javascript with getBid but it didn’t work...

1 answer

3


You can do it like this, I did it to show up at onchange select

$('#selectNames').on('change', function() {
  $('#myModal #nomePessoa').val($(this).find('option:selected').text());
  $('#myModal').modal('show');
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<select id="selectNames">
<option></option>
<option value="1">Miguel</option>
<option value="2">Alexandre José</option>
<option value="3">Sara</option>
</select>


<div id="myModal" class="modal fade" role="dialog">
              <div class="modal-dialog">

                <form action="enviar.php" method="post">

                    <!-- Modal content-->
                    <div class="modal-content">
                      <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                        <h4 class="modal-title">Cadastrar Nova Entrada</h4>
                      </div>
                      <div class="modal-body">
                        <p>Nome Pessoa</p>
                        <input type="text" name="nomePessoa" id="nomePessoa" value="">
                      </div>
                      <div class="modal-footer">
                        <button type="submit" class="btn btn-success" >Save</button>
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                      </div>
                    </div>

                </form>


              </div>
            </div>

EXAMPLE in jsfiddle

Browser other questions tagged

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