Take data from PHP modal window

Asked

Viewed 794 times

1

When clicking on a unit a table is opened inside the modal that from the user’s choice inserts the desired line in the input. But when I click on any row the whole table value is inserted. Someone knows what to change in javascript ?

<div class="container">
                <h2>Modal</h2>
                <!-- Trigger the modal with a button -->
                        <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Unidade</button>
                        <input type="text" class="form-control unidade">
                        <!-- Modal -->
                        <div class="modal fade" id="myModal" role="dialog">
                            <div class="modal-dialog">

                                <!-- Modal content-->
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                                        <label class="modal-title">UNIDADE</label>

                                    </div>
                                    <div class="modal-body">
                                        <table id="unidade">
                                            <?php
                                                  include ("conn.php");

                                                        echo "<table border = 2>";
                                                        echo "<tr>";
                                                        echo "<th>Unidade</th>";
                                                        echo "<th>Descrição</th>";
                                                        echo "</tr>";

                                                    $result = "SELECT codigo, descricao FROM cadunid";
                                                    $resultado = mysqli_query($conn, $result);

                                                    while($row = mysqli_fetch_assoc($resultado)) {
                                                        echo "<tr>";
                                                        echo "<td value='unidade'>". $row['codigo'] ."</td>";
                                                        echo "<td>". $row['descricao'] ."</td>";
                                                        echo "</tr>";
                                                    }
                                                ?>
                                        </table>
                                    </div>
                                    <div class="modal-footer">
                                        <button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
                                    </div>
                                </div>

                            </div>
                        </div>
                    </div>

                    <script>
                        $(document).on('click', 'table', function() {
                            var value = $(this).val(unidade).text();
                            $('.close').trigger('click');
                            $('.unidade').val(value);
                        });
                    </script>
  • Where is the unit class in html? In the JS part there is a unit variable that is not defined tbm...

  • This is defined within the input

  • the unit variable defined in the input? which is the html code of the input. .

  • the code is this completely expensive, I just need to arrange the javascript to take the value of the columns code and description completely, and take only the value of the row I select, this is a modal of bootstrap, if it could not help a lot thank you

  • Code is wrong, there is up to tr that is not being opened, but anyway. No loop while defines a class for TD that is clickable. example: class="get-text" Then in javascript you do $('. get-text'). click(Function(){ var value = $(this). text(); $('. unit'). val(value); })

  • I checked the post and there is tr wrong, but thanks for the warning. in case I will have to create a Hidden input inside while to declare that class ?

  • You edited the post 3 minutes ago. Before the tr inside the while was wrong, now you’re right.

  • If it doesn’t work update the code and comment here that I come to look.

  • did not work, thanks I will remove the post

  • You made a mistake or you didn’t behave the way you wanted?

  • simply does not receive nor click , I must be missing in the while loop but I give up

  • Use the answer I posted, it works. When you click on the td with the get-value class it takes its value and puts it in the input.

Show 7 more comments

1 answer

1


<div class="container">
                <h2>Modal</h2>
                <!-- Trigger the modal with a button -->
                        <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Unidade</button>
                        <input type="text" class="form-control unidade">
                        <!-- Modal -->
                        <div class="modal fade" id="myModal" role="dialog">
                            <div class="modal-dialog">

                                <!-- Modal content-->
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                                        <label class="modal-title">UNIDADE</label>

                                    </div>
                                    <div class="modal-body">
                                        <table id="unidade">
                                            <?php
                                                  include ("conn.php");

                                                        echo "<table border = 2>";
                                                        echo "<tr>";
                                                        echo "<th>Unidade</th>";
                                                        echo "<th>Descrição</th>";
                                                        echo "</tr>";

                                                    $result = "SELECT codigo, descricao FROM cadunid";
                                                    $resultado = mysqli_query($conn, $result);

                                                    while($row = mysqli_fetch_assoc($resultado)) {
                                                        echo "<tr>";
                                                        echo "<td class='get-value'>". $row['codigo'] ."</td>";
                                                        echo "<td>". $row['descricao'] ."</td>";
                                                        echo "</tr>";
                                                    }
                                                ?>
                                        </table>
                                    </div>
                                    <div class="modal-footer">
                                        <button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
                                    </div>
                                </div>

                            </div>
                        </div>
                    </div>



     <script>
                        $(document).on('click', '.get-value', function() {
                            var value = $(this).text();
                            $('.unidade').val(value);
                        });
                    </script>
  • now it worked, added the line $('.close'). Trigger('click') on top of the line $('unit'). val(value); and it worked. I just need to tinker with <td> now, for the whole line to be selectable and just get the code field

Browser other questions tagged

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