Html 表格中的项目删除按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1949083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:40:03  来源:igfitidea点击:

Item delete buttons in a table?

html

提问by Tominator

this is a simple html question but I would like to know the correct way of doing it.

这是一个简单的 html 问题,但我想知道正确的方法。

I have a table in my page, with tabular data, and it's a display of items in the database. I build the table dynamically with php, and just output html.

我的页面中有一个表格,带有表格数据,它是数据库中项目的显示。我用php动态建表,只输出html。

I want to add a column "delete this entry" with a button/link/picture on every line, and when it's clicked, my page refreshes and php knows which itemID should be deleted.

我想添加一个列“删除此条目”,每一行都有一个按钮/链接/图片,当它被点击时,我的页面会刷新,php 知道应该删除哪个 itemID。

The question is not about php, but about the html part.

问题不是关于 php,而是关于 html 部分。

How can I make the delete-button compliant with standards? Here's an incorrect solution:

如何使删除按钮符合标准?这是一个不正确的解决方案:

<table>
<form action='#' method='post'>
<tr>
<td>Item 1</td>
<td><input type='hidden' name='id' value='1'><input type='submit' value='X'></td>
</tr>
</form>
<form action='#' method='post'>
<tr>
<td>Item 2</td>
<td><input type='hidden' name='id' value='2'><input type='submit' value='X'></td>
</tr>
</form>
</table>

Anyone an idea on a good solution? I'd prefer not to use much javascript, if it can be avoided. Thanks in advance!

有人对一个好的解决方案有想法吗?如果可以避免的话,我宁愿不使用太多的 javascript。提前致谢!

Edit: actually, I have a secret second question too: what if i were to want to do inline editing (after pressing an Edit button), as in: make the cells with the values contain boxes, and a submit button at the last cell?

编辑:实际上,我还有一个秘密的第二个问题:如果我想进行内联编辑(按下“编辑”按钮后)怎么办,例如:使具有值的单元格包含框,并在最后一个单元格处添加一个提交按钮?

采纳答案by Sampson

You could go with a simple authenticatedGET procedure, like this:

您可以使用简单的经过身份验证的GET 过程,如下所示:

<a href="showResults.php?action=delete&item=112">Delete</a>

Note the actionand itemvalues in the URL. These would be evaluated before the database is called and the results are printed in showResults.php. Your PHP (on showResults.php) would start with a block of logic that checks to see if any actions were requested, such as delete. If an action is present, it checks to see if an itemis as well. I will then do whatever authentication necessary to ensure the user has sufficient rights to do this action to this item.

请注意URL 中的actionitem值。这些将在调用数据库之前进行评估,并将结果打印在showResults.php. 您的 PHP(在 showResults.php 上)将从一个逻辑块开始,用于检查是否请求了任何操作,例如delete。如果一个动作存在,它会检查一个项目是否也存在。然后,我将执行任何必要的身份验证,以确保用户有足够的权限对​​此项执行此操作。

Next it would query the database (now lacking said item assuming the deletion was successful) and redraw the page. The links would be built within your loop used to create the many table-rows:

接下来它将查询数据库(假设删除成功,现在缺少所述项目)并重绘页面。链接将在用于创建许多表行的循环中构建:

<?php foreach ($products as $product) { ?>
<tr>
  <td>
    <a href="sr.php?action=delete&item=<?php print $product->id; ?>">Delete</a>
  </td>
  <td>
    <p><?php print $product->title; ?></p>
  </td>
</tr>
<?php } ?>

回答by Agent_9191

Why not isolate the forms and pass the ID to the action page?

为什么不隔离表单并将 ID 传递给操作页面?

<table>
    <tr>
        <td>Item 1</td>
        <td><form action='action.php?id=1' method='post'><input type='submit' value='X'></form></td>
    </tr>
    <tr>
        <td>Item 2</td>
        <td><form action='action.php?id=2' method='post'><input type='submit' value='X'></form></td>
    </tr>
</table>

回答by Kolar?ík Václav

You can write names of submit buttons as set of facts and their values, for example name='set1(fact1:value2,fact2:value2,...)'. Then search for submitted set and get fact value. A PHP class PageFactsbelow can do this. You can check if set is posted using condition if (PageFacts::is('del'))and get value of fact with statement $itemID = PageFacts::get('del', 'id'). Form with submit buttons can look like:

您可以将提交按钮的名称写为一组事实及其值,例如name='set1(fact1:value2,fact2:value2,...)'。然后搜索提交的集合并获取事实值。下面的 PHP 类PageFacts可以做到这一点。您可以使用条件if (PageFacts::is('del'))检查 set 是否已发布,并使用语句$itemID = PageFacts::get('del', 'id') 获取事实值。带有提交按钮的表单看起来像:

<form method="post">
    ... item [0] text/input
    <input type="submit" name="del(id:0)" value="Del">
    ... item [1] text/input
    <input type="submit" name="del(id:1)" value="Del">
    ... item [2] text/input
    <input type="submit" name="del(id:2)" value="Del">
    ...
    ...
    <input type="submit" name="chng" value="Chng">
</form>

An example of PHP code that deletes table item with itemID can be:

删除带有 itemID 的表项的 PHP 代码示例可以是:

if (PageFacts::is('del')) {
    $itemID = PageFacts::get('del', 'id');
    // Delete $itemID
    deleteTableItem($itemID);
}

// or for multiple facts like del(cid:2,id:0)
if (PageFacts::is('del')) {
    $cartID = PageFacts::get('del', 'cid');
    $itemID = PageFacts::get('del', 'id');

    // Delete $itemID from $cartID
    deleteCartItem($cartID, $itemID);
}

PageFactsPHP code:

PageFactsPHP 代码:

class PageFacts {

    // Gets fact value from posted set
    // $s ... name of a set
    // $f ... name of a fact
    // returns - value of fact $f in set $s
    //         - TRUE  if set $s exists but fact $f doesn't exist
    //         - FALSE if set $s doesn't exist
    public static function get($s, $f = NULL) {
        // Regex pattern to search for set $s
        $ps = "/^\s*$s\s*\((.*)\)\s*$/";
        // Search POSTed names (variable $v is not used)
        foreach ($_POST as $key => $v) {
            $ok = preg_match($ps, $key, $matches);
            if ($ok && isset($matches[1])) {
                // If $f is not passed return TRUE, means set $s exists regardless of fact $f
                if (!isset($f)) {
                    return TRUE;
                }
                // Otherwise return value of fact $f
                // use regex pattern to search in list of fact:value items separated by a comma
                $pf = "/^\s*$f\s*\:\s*(.+)\s*$/";
                foreach (explode(',', $matches[1]) as $fv) {
                    $ok = preg_match($pf, $fv, $matches);
                    if ($ok && isset($matches[1])) {
                        return $matches[1];
                    }
                }
            }
        }
        return FALSE;
    }
    // Checks if set $s is posted
    public static function is($s) {
        return self::get($s);
    }
}

回答by munch

It's kind of messy, but going off of what Agent said, you could do something like this to get a text input in there too:

这有点乱,但是根据 Agent 的说法,您也可以执行以下操作来获取文本输入:

<form action='action.php?id=1' method='POST'>
<table>
    <tr>
       <td>Item 1</td>
       <td><input type="text" value="" /></td>
       <td><input type='submit' value='X'></td>
    </tr>
</table>
</form>

<form action='action.php?id=2' method='POST'>
<table>
    <tr>
       <td>Item 2</td>
       <td><input type="text" value="" /></td>
       <td><input type='submit' value='X'></td>
    </tr>
</table>
</form>

回答by Juraj Blahunka

Add a submit buttonto each form and send the operation you want to execute as value attribute of each button

为每个表单添加一个提交按钮,并将您要执行的操作作为每个按钮的值属性发送

<table>
    <form action='#' method='post'>
        <tr>
            <td>Item 1</td>
            <td><input type='hidden' name='id' value='1'><input type='submit' value='X'></td>
            <td><button type="submit" value="remove" name="op">Remove</button></td>
            <td><button type="submit" value="edit" name="op">Edit</button></td>
        </tr>
    </form>
</table>