0

I have the problem that my code in a while loop is without the form tag in the first run but in the following runs it's there.

    <?php
        $num = 1;
        while ($row = mysqli_fetch_object($daten)) {
            echo "<tr>";
                echo "<th>". $num. "</th>";
                echo "<td>". htmlspecialchars($row->vname)." ".htmlspecialchars($row->nname)."</td>";
                echo "<td>". htmlspecialchars($row->mail)."</td>";
                echo "<td>"; ?>
                    <form action='/resources/user_loeschen.php' method='post'>
                        <input type='hidden' name='mail' value='<?php echo htmlspecialchars($row->mail); ?>'>
                        <a onclick='this.parentElement.submit();'><img class='delete' src='resources/img/glyphicons-17-bin.png' height='25px'></a>
                    </form>
                </td>
            </tr>
            <?php
            $num++;
        } ?>

so this is the code in the while loop. The form should be always there. The only issue I can imagine is that the while loop itself is in a form too. But the fact that just the first run of the loop fails and the following runs work absolutely fine is a good proof that it can work.

the result html:

    <tr>
        <th>1</th>
        <td>First Test</td>
        <td>test.first@test.com</td>
        <td>
            <input type='hidden' name='mail' value='test.first@test.com'>
            <a onclick='this.parentElement.submit();'><img class='delete' src='resources/img/glyphicons-17-bin.png' height='25px'></a>
        </td>
     </tr>
     <tr>
        <th>2</th>
        <td>Test User</td>
        <td>test.user@test.com</td>
        <td>
            <form action='/resources/user_loeschen.php' method='post'>
                <input type='hidden' name='mail' value='test.user@test.com'>
                <a onclick='this.parentElement.submit();'><img class='delete' src='resources/img/glyphicons-17-bin.png' height='25px'></a>
            </form>
        </td>
    </tr>

Thanks in advance.

mannaris
  • 51
  • 8
  • 2
    Can you please provide result HTML code ? just the first who fails ! – Goms Aug 01 '18 at 19:52
  • when you say "without the form tag", do you really mean that _none_ of the HTML in the loop is rendered, or just literally the rest of it is there but without the form tag? I don't really see how the latter could be possible, tbh. The first scenario also seems a bit unlikely, unless there's a warning from the PHP or something is being rendered which is screwing up the HTML. Might be best to show us the rendered HTML. – ADyson Aug 01 '18 at 19:53
  • @ADyson no, just the form opening and closing tag are missing. – mannaris Aug 01 '18 at 20:11
  • sorry, but to my eyes it seems impossible for the code you've posted to produce that result. Are you 100% certain that's the right code? – ADyson Aug 01 '18 at 20:13
  • Yes, I am, but I think I found the mistake. As I said: the while loop is in a form tag too, so there are forms inside of forms and thats forbidden. I think the reason for the following runs to success is, that the first run brings up a bug that the browser registers the mistake only once (the first run) and the other forms still get through. – mannaris Aug 01 '18 at 20:17
  • @Avothr interesting. yes, nested forms are not allowed, but odd that it should allow subsequent nested forms. Which browser is this being tested in, btw? I would suspect that maybe different browsers treat this scenario somewhat differently. Some might allow the form to be rendered but simply stop it from working. – ADyson Aug 01 '18 at 20:28
  • @ADyson I use Firefox, but Chrome and Edge do the **exact** same thing. This is somewhat stupid o.O – mannaris Aug 01 '18 at 20:45
  • True. I bet none of the subsidiary forms work though, since they're all nested? The fact the first one isn't rendered at all is a bit odd, but it doesn't fundamentally affect the functionality, which is really the important thing. – ADyson Aug 01 '18 at 20:46
  • All forms that are displayed work! So the parent form and the forms of the following runs all work! only the not displayed one dosn't work, because it's not displayed. – mannaris Aug 01 '18 at 20:48
  • 1
    That's really odd IMO. I wonder if you've found a hole in these browsers implementation of the HTML spec? Seems a fairly obvious one, and it has been noticed before: https://stackoverflow.com/a/30975003/5947043 . I wouldn't expect it to last forever. – ADyson Aug 01 '18 at 21:20
  • Where did you get the resulting HTML from? Is this really the direct output from your PHP script/server, or some kind of sanitized code grabbed from some browser? Its impossible that your loop produces different results in different iterations without an if/else block or something similar. – Christian Engel Aug 01 '18 at 21:37
  • It's out of the browser, please read the comments above. – mannaris Aug 01 '18 at 21:38
  • @ChristianEngel unfortunately the code snippet above is incomplete. Due to a form further up the DOM tree into which this code is nested, the browser detects the first nested form and simply removes it (correctly, as nested forms aren't allowed). However, as we've been discussing, it fails to remove the subsequent ones even though it probably should. Please read all the comments before commenting... :-) – ADyson Aug 01 '18 at 22:13
  • Thats what I meant! "The browser removed it" - this means the HTML you shown above is NOT the output of your PHP script, but the "cleaned" result from your browser. I have no idea how you were even able to retreive that result :) – Christian Engel Aug 02 '18 at 08:21

0 Answers0