How do I get the value of a select and fill in an input?

2

<?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><divclass="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>
    
asked by anonymous 01.07.2016 / 03:28

1 answer

3

You can do so, I did to appear in onchange of 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><scriptsrc="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

    
01.07.2016 / 10:46