猿问

为什么 td 标签内的提交按钮不起作用?

正如标题所示,td 内的“eliminaRecensioneBTN”按钮不起作用。我尝试将按钮放在标签之外并且它起作用了,但是如果我将该按钮放在 td 标签之外它就起作用了;我需要里面的那个。


PHP代码:


    <?php

    session_start();


    if (!(isset($_SESSION['autorizzato']) && $_SESSION['autorizzato'] == true)) {

        header("Location: login.php");

    }

    $nomepagina = 'sezioni';


    $conn = mysqli_connect('localhost', 'root', '', 'gestione');

    if (!$conn) {

        die("Connessione non riuscita: ".mysqli_connect_error());

    }


    function prendiRecensioni($conn){

        $sql = "SELECT * FROM recensioni";

        $result = $conn->query($sql);

        while($row = $result->fetch_assoc()){

            echo "<form method='POST' action='".eliminaRecensione($conn)."'>";

                echo "<tr>";

                    echo "<td>".$row['id']."</td>";

                    echo "<td>".$row['nomevisualizzato']."</td>";

                    echo "<td>".$row['pubblicazione']."</td>";

                    echo "<td>".mb_strimwidth($row['descrizione'], 0, 20, "...")."</td>";

                    echo "<input type='hidden' name='id' value='".$row['id']."' />";

                    echo "<td class='text-right'>";

                        echo "<button type='submit' name='eliminaRecensioneBTN' class='btn btn-danger btn-xs'><i class='fa fa-trash'></i>&nbsp;&nbsp;Elimina</button>";

                    echo "</td>";

                echo "</tr>";

            echo "</form>";

        }

    }


    function eliminaRecensione($conn){

        if(isset($_POST['eliminaRecensioneBTN'])){

            $id = $_POST['id'];

            $sql = "DELETE FROM recensioni WHERE id='$id'";

            $result = $conn->query($sql);

            echo "<meta http-equiv='refresh' content='0'>";

        }

    }

?>

我不知道如何修复,那么我该如何修复呢?


慕田峪9158850
浏览 85回答 1
1回答

温温酱

action从标记中删除该属性<form>,以便表单将POST转到其所在的同一 URL。在文件顶部,检查表单是否已发布,并使用 SQL 查询删除该帖子。之后,获取结果并使用 HTML 显示表单。不要忘记在您的和标签周围放置一个<table>标签!<tr><td>将hidden输入放在标签底部<form>,位于<table>. 反正也不会显示出来!<?php$conn = mysqli_connect( 'localhost', 'root', '', 'gestione' );if ( ! $conn ) {&nbsp; &nbsp; die( "Connessione non riuscita: " . mysqli_connect_error() );}if ( isset( $_POST['eliminaRecensioneBTN'] ) && isset( $_POST['id'] ) ) {&nbsp; &nbsp; $conn->query( "DELETE FROM recensioni WHERE id='$_POST['id']'" );&nbsp; &nbsp; echo '<meta http-equiv="refresh" content="0">';}$result = $conn->query( 'SELECT * FROM recensioni' );?><?php while ( $row = $result->fetch_assoc() ): ?>&nbsp; &nbsp; <form method="POST">&nbsp; &nbsp; &nbsp; &nbsp; <table>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><?php echo $row['id']; ?></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><?php echo $row['nomevisualizzato']; ?></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><?php echo $row['pubblicazione']; ?></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><?php echo mb_strimwidth( $row['descrizione'], 0, 20, '...' ); ?></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td class="text-right">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button type="submit" name="eliminaRecensioneBTN" class="btn btn-danger btn-xs">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <i class="fa fa-trash"></i>&nbsp;&nbsp;Elimina&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </button>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; </table>&nbsp; &nbsp; &nbsp; &nbsp; <input type="hidden" name="id" value="<?php echo $row['id']; ?>" />&nbsp; &nbsp; </form><?php endwhile; ?>
随时随地看视频慕课网APP

相关分类

Html5
我要回答