在 PHP/HTML 中创建视图按钮

发布于 2024-10-18 22:25:45 字数 1392 浏览 2 评论 0原文

我的“服务”有一个数据库表,其中包含 serviceid 和其他一些不相关的字段。我正在使用 PHP 动态生成显示查询服务的 HTML 表。每行都有一个相应的“查看”按钮,单击该按钮后应提交到具有要查看的服务 id 值的同一页面(在设置 $_POST[ 后,该页面将重定向到另一个页面) 'serviceid'] 等于从按钮传递的值)。

以下是我如何使用 PHP 动态创建 HTML 表:

    while(list($id, $name, $details, $datecreated, $firstname, $lastname) = $results->fetch_row())
    {
      if($index % 2 == 0)
      {
        echo "<tr BGCOLOR=\"#DCDCDC\" style=\"color: Black;\">";
      }
      else
      {
        echo "<tr>";
      }
      echo "<td>" . "<input type=\"checkbox\" id=\"flag$id \" name=\"flag\" value=\"Flag$id\"/>" . "</td>";
      echo "<td>" . "<input type=\"submit\" id=\"view$id \" name=\"view\" value=\"View\"/>" . "</td>";
      echo "<td>" . $id . "</td>";
      echo "<td>" . $name . "</td>";
      echo "<td>" . $details . "</td>";
      echo "<td>" . $datecreated . "</td>";
      echo "<td>" . $firstname . " " . $lastname . "</td>";
      echo "</tr>";

      $index++;
    } 

这是我的 results.view.php,这是我显示数据的地方。虽然您看不到上面的

标记,但只要知道它回发到包含此视图和处理按钮按下等的模型的控制器就足够了。

我的问题是我如何知道哪个视图按钮被单击?它可能是在第一个服务、最后一个服务或介于两者之间的任何服务上。我已将服务的 $id 放入按钮的 id 值中,但我不确定如何引用它。如果我知道如何做到这一点,那么我就有可能拼接出“视图”一词,并留下执行任务所需的 id。

有什么想法吗?

I have a database table for my "Services" which contains a serviceid and some other non-related fields. I am using PHP to dynamically generate an HTML table that displays the queried services. In each of the rows is a corresponding "View" button that when clicked should submit to the same page with the value of the id of the service to be viewed (the page will then redirect to another page after setting $_POST['serviceid'] equal to the value passed from the button).

Here is how I am dynamically creating my HTML table with PHP:

    while(list($id, $name, $details, $datecreated, $firstname, $lastname) = $results->fetch_row())
    {
      if($index % 2 == 0)
      {
        echo "<tr BGCOLOR=\"#DCDCDC\" style=\"color: Black;\">";
      }
      else
      {
        echo "<tr>";
      }
      echo "<td>" . "<input type=\"checkbox\" id=\"flag$id \" name=\"flag\" value=\"Flag$id\"/>" . "</td>";
      echo "<td>" . "<input type=\"submit\" id=\"view$id \" name=\"view\" value=\"View\"/>" . "</td>";
      echo "<td>" . $id . "</td>";
      echo "<td>" . $name . "</td>";
      echo "<td>" . $details . "</td>";
      echo "<td>" . $datecreated . "</td>";
      echo "<td>" . $firstname . " " . $lastname . "</td>";
      echo "</tr>";

      $index++;
    } 

This is my results.view.php, this is where I display the data. While you can't see the <form> tag above it is sufficient to know that it posts back to the controller that includes this view and the model that handles the button presses and whatnot.

My question then is how do I know which view button is being clicked? It could be on the first service, the last one, or any in between. I have placed the $id of the service in the id value of the button, but I am unsure how to reference that. If I knew how to do that, then I could potentially splice out the word "view" and would be left with the id needed to perform the task.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昵称有卵用 2024-10-25 22:25:45

只有您单击的按钮的值才会发送到下一页。如果您为所有按钮分配唯一名称,则只需使用

if (isset($_POST['button1'])) echo "button 1 was clicked"; 检查它们。

当然,如果您在其中一个字段中使用 [Enter] 提交表单,则这将不起作用。另一种选择是为此使用单选按钮?或者更多形式。

Only the the value of button you've clicked will be sent to the next page. If you assign all the buttons unique names, you can then simply check for them with

if (isset($_POST['button1'])) echo "button 1 was clicked";.

Of course this won't work if you submit the form with [Enter] in one of the fields. Another option would be to use a radio button for this? Or more forms.

云淡月浅 2024-10-25 22:25:45

为您的按钮提供 ID 可以解决鼠标问题,但不适用于键盘;无论您的焦点在哪里,第一个提交按钮都会声明自己已按下。

我可以看到有两种解决方案:

  • 使用 A 元素和查询字符串。
  • 每个 TR 中都有一个 FORM 元素。

Giving your buttons àn ID will solve mouse issues, but it will not work with keyboards; The first submit button will declare itself pressed no matter of where your key focus is.

There is 2 solutions as I can see it:

  • use A elements instead, with an query-string.
  • a FORM element in every TR.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文