I have a problem with this code and i really can't understand what is it.. i have unique ID for every ticket and it works good but i have made a button to open a modal (#myModal) and from there to send the ticket to some username but the problem is no matter what ticket i choose to send just the first ticket code is shown also when i try to remove any ticket i can't, its just possible to remove the first ticket.

<tr style="background-color: lavender;">
    <th>
        <button type="submit" class="btn btn-primary btn-xs" name="share" value="<?= $ticket_id ?>" id="shareBtn" title="Share ticket" onclick="change()">Share</button>
        <button type="button" class="btn btn-danger btn-xs" data-toggle="modal" value="<?= $ticket_id ?>" data-target="#myModal" onclick="change()">Ticket ID: <?= $ticket_id ?></button>

        <div class="modal fade" id="myModal" role="dialog">
            <div class="modal-dialog modal-sm">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                        <h4 class="modal-title">Send Ticket (<?= $ticket_id ?>)</h4>
                    </div>
                    <div class="modal-body">
                        <p><input type="text" class="form-control" id="username" placeholder="Enter Username"/></p>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-success btn-block" data-dismiss="modal">Send</button>
                    </div>

                </div>
            </div>
        </div>
    </th>
    <th>
        <a type="submit" class="close" data-dismiss="panel" aria-label="close" id="hide" value="<?= $ticket_id ?>" title="Remove ticket" name="closeTicket" onclick="change()">&times;</a>
    </th>
</tr>
Member Avatar for diafol

You don't include the code change(). All buttons seem to be calling this. Is this snippet prt of a loop? WHy is it part of a table heading?

Oops.. that was a misstake i have deleted that it was there from before because the lines are copied btw i will pot the full script and yes it is in loop but i have the same script just without bootstrap and it works fine..

<?php 
        $responseText = '';
        $user = getUserData('users', 'UserUsername');
        if(isset($_POST['closeTicket'])) {
            $ticket_id = (int) $_POST['closeTicket'];
            $sql = "UPDATE `kladilnica` SET `isActive` = 0 WHERE `ticket_id` = '$ticket_id' AND `Username`='$user'";
            $return = $conn->query($sql);
            $responseText = ($conn->affected_rows)
            ? "<li class='SuccShared' id='activator'>Something went wrong</li>" 
            : "<li class='ticketRemoved' id='activator'>Ticket ($ticket_id) was removed successfully</li>";
        }
        if(isset($_POST['share'])) {
            $ticket_id = (int) $_POST['share'];
            $sql = "UPDATE `kladilnica` SET `isShared`=1 WHERE `ticket_id` = '$ticket_id' AND `Username`='$user'";
            $return = $conn->query($sql);
            $responseText = ($conn->affected_rows) 
            ? "<li class='SuccShared' id='activator'>You have successfully shared the ticket $ticket_id</li>" 
            : "<li class='AlreadyShared' id='activator'>The ticket ($ticket_id) was shared already</li>";

        }
        $sql = "SELECT `Uplata`, `Dobivka`, 
        `match1`, `match2`, `match3`, `match4`, 
        `match5`, `match6`, `match7`, `match8`, 
        `match9`, `match10`, `match11`, `match12`, 
        `match13`, `match14`, `match15`, `match16`, 
        `tip1`, `tip2`, `tip3`, `tip4`, 
        `tip5`, `tip6`, `tip7`, `tip8`, 
        `tip9`, `tip10`, `tip11`, `tip12`, 
        `tip13`, `tip14`, `tip15`, `tip16`, 
        `ticket_id`, `isActive`, `isShared` 
        FROM `kladilnica` 
        WHERE `Username`='$user'";

        $result = $conn->query($sql);
        $count = 0;
        $uplata_total = 0;

        if($result === false) {
            echo "<b>Could not connect to database.</b>";
            trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
        } else {
            if($result->num_rows == 0) {
                echo "<b>You do not have any tickets.</b>";
        } else if($result->num_rows >= 1) {
            $count = $count + 1;
            while($row = $result->fetch_assoc()) {
                if($result->num_rows != empty($row['match1'])) {
                    $uplata_total = $row["Uplata"];
                    $dobivka_total = $row["Dobivka"];
                    $ticket_id = $row["ticket_id"];
                    $is_active = $row["isActive"];
                    $shared = $row["isShared"];

                    if($is_active == 1) {
                    ?>
        <!-- TICKETS -->
            <form action='home.php' method='POST'>
                <div class="col-md-6 col-md-pull">
                    <div class="panel panel-default" id="ticketPanel">
                        <table class="table table-hover">
                            <thead>
                                <tr style="background-color: lavender;">
                                    <th>
                                        <button type="submit" class="btn btn-primary btn-xs" name="share" value="<?= $ticket_id ?>" id="shareBtn" title="Share ticket">Share</button>
                                        <button type="button" class="btn btn-danger btn-xs" data-toggle="modal" value="<?= $ticket_id ?>" data-target="#myModal">Ticket ID: <?= $ticket_id ?></button>

                                    </th>
                                    <th>
                                        <a type="submit" class="close" data-dismiss="panel" aria-label="close" id="hide" value="<?= $ticket_id ?>" title="Remove ticket" name="closeTicket">&times;</a>
                                    </th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td><?= $row["match1"] ?></td>
                                    <td><?= $row["tip1"] ?></td>
                                </tr>
                                <tr>
                                    <td><?= $row["match2"] ?></td>
                                    <td><?= $row["tip2"] ?></td>
                                </tr>
                                <tr>
                                    <td><?= $row["match3"] ?></td>
                                    <td><?= $row["tip3"] ?></td>
                                    </tr>
                                <tr>
                                    <td><?= $row["match4"] ?></td>
                                    <td><?= $row["tip4"] ?></td>
                                </tr>
                                <tr>
                                    <td><?= $row["match5"] ?></td>
                                    <td><?= $row["tip5"] ?></td>
                                </tr>
                                <tr>
                                    <td><?= $row["match6"] ?></td>
                                    <td><?= $row["tip6"] ?></td>
                                </tr>
                                <tr>
                                    <td><?= $row["match7"] ?></td>
                                    <td><?= $row["tip7"] ?></td>
                                </tr>
                                <tr>
                                    <td><?= $row["match8"] ?></td>
                                    <td><?= $row["tip8"] ?></td>
                                </tr>
                                <tr>
                                    <td><?= $row["match9"] ?></td>
                                    <td><?= $row["tip9"] ?></td>
                                </tr>
                                <tr>
                                    <td><?= $row["match10"] ?></td>
                                    <td><?= $row["tip10"] ?></td>
                                </tr>
                                <tr>
                                    <td><?= $row["match11"] ?></td>
                                    <td><?= $row["tip11"] ?></td>
                                </tr>
                                <tr>
                                    <td><?= $row["match12"] ?></td>
                                    <td><?= $row["tip12"] ?></td>
                                </tr>
                                <tr>
                                    <td><?= $row["match13"] ?></td>
                                    <td><?= $row["tip13"] ?></td>
                                </tr>
                                <tr>
                                    <td><?= $row["match14"] ?></td>
                                    <td><?= $row["tip14"] ?></td>
                                </tr>
                                <tr>
                                    <td><?= $row["match15"] ?></td>
                                    <td><?= $row["tip15"] ?></td>
                                </tr>
                                <tr>
                                    <td><?= $row["match16"] ?></td>
                                    <td><?= $row["tip16"] ?></td>
                                </tr>
                                <tr style="background-color: lightgreen;">
                                    <th>Total Bet: <?= $uplata_total ?></th>
                                    <th>Total Win: <?= $dobivka_total ?></th>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
                <?php
                    if($count % 10 == 0) {
                        ?>
                        <div id="ticketads">
                            <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
                            <!-- sharedAds -->
                            <ins class="adsbygoogle"
                                 style="display:inline-block;width:280px;height:250px"
                                 data-ad-client="ca-pub-5699416235054188"
                                 data-ad-slot="8461367752"></ins>
                            <script>
                            (adsbygoogle = window.adsbygoogle || []).push({});
                            </script>
                        </div>
                        <?php
                    }
                ?>

                <div class="modal fade" id="myModal" role="dialog">
                    <div class="modal-dialog modal-sm">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal">&times;</button>
                                <h4 class="modal-title">Send Ticket (<?= $ticket_id ?>)</h4>
                            </div>
                            <div class="modal-body">
                                <p><input type="text" class="form-control" id="username" placeholder="Enter Username"/></p>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-success btn-block" data-dismiss="modal">Send</button>
                            </div>
                        </div>
                    </div>
                </div>

            </form>
            <?php
                    $count++;
                    }
                }
            }
        }
    }
    $conn->close();
    echo $responseText;
    ?>     

    </div>
Member Avatar for diafol

OK quite a few things here stef:

THe main thing I see is the while loop. This gives you multiple modal forms and multiple GoogleAd <script> tags. Is this intentional? This really does not need to happen with the modal.

You can have a hidden modal outside the loop that gets called and then either copy data to it from a json object or from data attributes from the calling element OR get ajax info via the calling element (the 'id').

This is really hard to maintain - the mix of markup and php and js. You would probably be better off handing off the table to a template file. I recently wrote a reply for something similar. But I'll only look if you're at all interested.

Bootstrap, although really useful - and I quite like it - produces so much ridiculous HTML, that maintaining it with placeholders (i.e. creating Views / templates) is almost the only way I find sane.

But the same code works on another file here it is...

<form action='home.php' method='POST'>
<table>
    <tr>
        <td>
            <table id='t01'>
                <thead>
                    <tr>
                        <th colspan='2'>
                            <button name="share" class="share_button" value="<?= $ticket_id ?>" type="submit" id="shareBtn" title="Share ticket" onclick="change()">Share</button>
                            <button name="closeTicket" class="close_tic_btn" type="submit" value="<?= $ticket_id ?>" id="shareBtn" title="Remove ticket" onclick="change()">X</button>
                        </th>
                    </tr>
                    <tr>
                        <th colspan='2'>
                            Ticket <?= $count ?>
                            <a style='position: relative; float: right;'>Ticket ID: <?= $ticket_id ?></a>
                        </th>
                    </tr>
                </thead>
                <tbody>
                <tr>
                    <td><?= $row["match1"] ?></td>
                    <td><?= $row["tip1"] ?></td>
                </tr>
                <tr>
                    <td><?= $row["match2"] ?></td>
                    <td><?= $row["tip2"] ?></td>
                </tr>
                <tr>
                    <td><?= $row["match3"] ?></td>
                    <td><?= $row["tip3"] ?></td>
                </tr>
                <tr>
                    <td><?= $row["match4"] ?></td>
                    <td><?= $row["tip4"] ?></td>
                </tr>
                <tr>
                    <td><?= $row["match5"] ?></td>
                    <td><?= $row["tip5"] ?></td>
                </tr>
                <tr>
                    <td><?= $row["match6"] ?></td>
                    <td><?= $row["tip6"] ?></td>
                </tr>
                <tr>
                    <td><?= $row["match7"] ?></td>
                    <td><?= $row["tip7"] ?></td>
                </tr>
                <tr>
                    <td><?= $row["match8"] ?></td>
                    <td><?= $row["tip8"] ?></td>
                </tr>
                <tr>
                    <td><?= $row["match9"] ?></td>
                    <td><?= $row["tip9"] ?></td>
                </tr>
                <tr>
                    <td><?= $row["match10"] ?></td>
                    <td><?= $row["tip10"] ?></td>
                </tr>
                <tr>
                    <td><?= $row["match11"] ?></td>
                    <td><?= $row["tip11"] ?></td>
                </tr>
                <tr>
                    <td><?= $row["match12"] ?></td>
                    <td><?= $row["tip12"] ?></td>
                </tr>
                <tr>
                    <td><?= $row["match13"] ?></td>
                    <td><?= $row["tip13"] ?></td>
                </tr>
                <tr>
                    <td><?= $row["match14"] ?></td>
                    <td><?= $row["tip14"] ?></td>
                </tr>
                <tr>
                    <td><?= $row["match15"] ?></td>
                    <td><?= $row["tip15"] ?></td>
                </tr>
                <tr>
                    <td><?= $row["match16"] ?></td>
                    <td><?= $row["tip16"] ?></td>
                </tr>
                <tr>
                    <th colspan='2'>Total Bet: <?= $uplata_total ?></th>
                </tr>
                <tr>
                    <th colspan='2'>Total Win: <?= $dobivka_total ?></th>
                </tr>
            </tbody>
        </table>
    </td>
</tr>
</table>
</form>
Member Avatar for diafol

your issue by the sounds of it stem from having many modals, all with the same id. As I mentioned have ONE modal outside the loop. Sorry can't get more in depth as am on vacation.

Actually i have two modals in this file, first is on the top of the body and have nothing to do with the loop and the second is in the loop but as you said remove from the loop so i put it on the bottom after the loop ending, so now its showing the last added ticetID :D

Member Avatar for diafol

Obviously as you're had-coding the last record info into it. You need to set up some javscript so when you click a certain button, the id gets copied to the modal, if that's what you need. I'm sure I mentioned this before?

Okay thanks, i think i've got it.. i need to define panel id which will be the ticketID and from there i need JavaScript to hide the panel if that's the solution i have one question. Am i alowed to use PHP in div id for ex. <div class="panel panel-default" id="<?php $ticket_id ?>"> and than to use it in JavaScript like this

$(document).ready(function() {
    $('#hide').on('click', function(e) { 
        $('$ticket_id').remove();
    });
});

i tryed this but no working... if you can help me about this it would be great thank you very much :)

Stefan_1 would be great if you read once again what HTML is , what is JavaScript and what is server side programming languages like PHP (believe me this could help you a lot more than just solving this). I started writing an example in this answer but then I noticed that you don't really need anything from PHP in that part of your code , probably you need an id just to load the proper info to the modal window. Remember ONE modal window per scope and the contents are changed per this scope id (or img src or whatever unique identifier) (I believe diafol was clear to that) . Do you need an example describing from the beginning of how such things work ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.