首页 > 解决方案 > PHP 看不到

问题描述

当我单击删除时,没有任何反应。

为什么在 PHP 中看不到提交?

这是我的功能

// this code after query and fetch
echo "<table border='1'>
       <form method='POST'>
        <tr>
        <th>Books</th>
        <th>Action</th>
        </tr>";
        foreach($result as $row){
            echo "<tr>";
            echo "<td>" . $row['nameOfBook'] . "</td>";
            echo "<td>" ."<input type='submit' name='delete' value='Delete' method='post' >" . "</td>";
            echo "</tr>";
        }
        
        echo "</table>";
        echo "</form";
       
        if(isset($_POST['delete'])){
            die("SS");
        }

标签: phphtmlmysqli

解决方案


缺陷字符列表;
来自评论;您没有使用>or关闭您的 html 输入标记/>

您使用该变量$statment,但这从未在您的函数中定义,从它调用的函数的名称(以及它被标记的事实!),看起来您正在使用 mysqli,所以添加

$statment = mysqli_query($connectin, "your query here");
$statment->fetchAll();

将有助于解决该问题,甚至传递$statment到函数
中这最终是您的主要问题

确保您在输入周围使用表单标签,并为其提供一些关于您要删除的内容的定义功能,否则它可能会删除所有

您添加method="post"到您的输入 - 表单应该在哪里,表单包含方法而不是输入,它们仅用于数据门/发送

如果这是我这样做——而不是“提交”类型的输入,我会使用一个按钮并有一个 JavaScript (jQuery) onclick 事件,例如;

<?
function getBooks($start = 0, $limit = 2)
{
    global $connection; // Add your link into this to do queries
    $sql_start = $start * $limit;
    $sql_limit = $limit;
    // Do your query and define $statment
    $statment = mysqli_query($connection, "SELECT something FROM a_table");
    $result = $statment->fetchAll();

    // Rather than printing lots separate, add to a return value and do this once
    $return_value = "";
    $return_value .= "<table border='1'>
    <tr>
    <th>Books</th>
    <th>Action</th>
    </tr>";
    foreach($result as $row){
        $return_value .= "<tr>";
        $return_value .= "<td>" . $row['nameOfBook'] . "</td>";
        // Rather than have "submit", use a button for this and add an onclick event
        $return_value .= "<td>" ."<input type='button' name='delete' value='Delete' onclick='deleteBook({$row['bookid']})' />" . "</td>";
        $return_value .= "</tr>";
    }
    $return_value .= "</table>";

    if(isset($_POST['book_to_delete'])){
        die("SS");
    }

    return $return_value;
}
?>

<form method="POST" action="#" id="book_list">
    <!-- Add a hidden input to hold the value you want to delete -->
    <input type="hidden" id="book_to_delete" name="book_to_delete" />
    <?
    print getBooks();
    ?>
</form>

<script>
    function deleteBook(bookId)
    {
        // Set the book_to_delete and submit the form with this
        jQuery("#book_to_delete").val(bookId);
        jQuery("#book_list").submit();
    }
</script>

推荐阅读