【问题标题】:Why delete button voids edit button execution为什么删除按钮无效编辑按钮执行
【发布时间】:2015-12-09 01:30:15
【问题描述】:

我在表格中有数据库生成的内容,在行尾的表格中有编辑和删除按钮。

<td>
    <form action="?" method="post">
        <div class="">
            <input type="hidden" name="id" value="<?php htmlspecialchars($item['id']); ?>">

            <input type="hidden" name="action" value="edit_daily_shift_report">
            <input type="submit" value="Edit" onclick="return confirm('Edit this report?');"> 

            <input type="hidden" name="action" value="delete_daily_shift_report">
            <input type="submit" value="Delete" onclick="return confirm('Delete this report?');"> 
        </div>
    </form>
</td>

如果我删除删除按钮,编辑代码可以正常工作。但是在两个按钮都存在的情况下,编辑按钮会失败,并且该行中的项目被删除。我难住了。不仅编辑按钮的动作值被忽略,删除按钮的动作值也被执行。任何帮助表示赞赏!

这里是控制器编辑和删除代码:

/***********************  Edit Daily Shift Report  ************************/
if (isset($_POST['action']) && $_POST['action'] === 'edit_daily_shift_report')
{
    include '../includes/dbconnect-local.php';

    try 
    {
       $sql = 'SELECT * FROM daily_shift_report WHERE id = :id'; 
       $s = $db->prepare($sql);
       $s->bindValue(':id', $_POST['id']);
       $s->execute();
    }
    catch (PDOException $e)
    {
        $error = 'Error deleting data.' . $e->getMessage();
        include 'error.html.php';
        exit();
    }

    // Assign page title value
    $pageTitle = 'Edit Daily Shift Report';

    // Store single row resut in $item associative array
    $item = $s->fetch(PDO::FETCH_ASSOC);

    // Display report content in form
    include 'edit_daily_shift_report.html.php';
    exit();
}

/*********************  Delete from Daily Shift Report  *******************/
if (isset($_POST['action']) && $_POST['action'] === 'delete_daily_shift_report')
{
    include '../includes/dbconnect-local.php';

    try 
    {
       $sql = 'DELETE FROM daily_shift_report WHERE id = :id'; 
       $s = $db->prepare($sql);
       $s->bindValue(':id', $_POST['id']);
       $s->execute();
    }
    catch (PDOException $e)
    {
        $error = 'Error deleting data.' . $e->getMessage();
        include 'error.html.php';
        exit();
    }

    echo '<span style="color:#ff0000;">Daily Shift Report DELETED successfully!</span>';
}

谢谢。

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    您需要了解发布请求的工作原理。如您所知,您有两个action 字段,一个用于删除,一个用于编辑。 简而言之,您的问题是没有办法将康涅狄格特定的输入字段输入到不同的按钮。

    我宁愿建议您将按钮的名称设置为action,并将值设置为您已经用于隐藏字段的值。 有了这个,你还必须在你的 html 中做一个小的改变。而不是:

    <input type="submit" value="something" onclick="something">
    

    使用这个:

    <button name="action" value="edit" onclick="something">Edit</button>
    

    删除按钮也是如此

    当你使用button标签而不是input时,你可以为按钮设置一个不同于显示的值,这样在之后检查$_POST['action']的值时,它的PHP代码会更简洁

    【讨论】:

    • 有一件事我忘了说。当您有同名的字段时,该值将被下一个同名的字段覆盖。一个例外是如果您在名称中使用 []。 Php 将其视为一个数组,并将为您创建该字段和数组
    • 谢谢@Morten!很抱歉我的回复延迟。感谢您花时间看看这个。在您的解决方案之前,我通过创建两个表单并将每个表单放入自己的 来解决它。这似乎不是最好的解决方案,但它确实有效。但是,您的解决方案更好,我在这里和其他地方都使用过它。使用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-15
    • 1970-01-01
    • 2021-04-18
    • 1970-01-01
    • 2019-09-06
    • 2011-11-05
    • 1970-01-01
    相关资源
    最近更新 更多