【问题标题】:How to check button is clicked or not in PHP?如何在 PHP 中检查按钮是否被点击?
【发布时间】:2018-12-31 08:02:51
【问题描述】:

我已经搜索过这个问题,但所有解决方案都与 javaScript 相关。我需要检查一个按钮是否被点击。按钮 (<a href="something"></a>) 也链接到其他页面。我已经尝试过,但我的代码适用于任何人查看该页面时。我想要任何人点击查看按钮。我的代码:

 $for_linking = preg_replace(array('/\.[A-Z]*/i') , '', $orders->order_file);

 if (isset($_GET['link'])) {
    $link = $_GET['link'];
    if($_SERVER['PHP_SELF'] == $for_linking){
        $ip_address = gethostbyname(trim(`hostname`));
        $pc_username = getenv("username");

        DB::table('click_counts')->insertGetId([
                 'ip' => $ip_address, 
                  'username' => $pc_username, 
                  'order_id' => $orders->id,
         ]);
    }
 }

 return '<a href="' . url("file").'/'.$orders->order_file.'/?link='.$for_linking.'" class="btn btn-xs btn-primary">View</a>'; 

我使用数据表来显示数据。如果我不使用if 条件,那么当我查看页面时正在插入数据。但我希望当有人点击View 时,数据将被插入。那么我可以在这里使用 if 条件吗?

以下代码仅适用于查看页面:

    $ip_address = gethostbyname(trim(`hostname`));
    $pc_username = getenv("username");

    DB::table('click_counts')->insertGetId([
             'ip' => $ip_address, 
              'username' => $pc_username, 
              'order_id' => $orders->id,
     ]);

    return '<a href="' . url("file").'/'.$orders->order_file.'" class="btn btn-xs btn-primary">View</a>'; 

表格:

【问题讨论】:

  • 那你为什么不使用 ajax?
  • @m.elewa 我没有太多使用 ajax。
  • 我真的没有得到你想要达到的效果,你能把这个解释清楚一点吗?
  • Ajax 将是您的最佳选择。
  • @maio290 我想在任何人点击查看按钮时将一些数据(ip &amp; pc username)插入数据库(查看表格图片)。

标签: php laravel datatable


【解决方案1】:

你可以在按钮上使用参数

<a href="something?action=send&id=XX"></a>

在页面上的 php 脚本中,您使用 $_GET 进行操作。

$action = '';
if(isset($_GET['action'])) {
    $action = $_GET['action'];
}
if($action == 'send') {
    // the send buttons was clicked
    $id = $_GET['id'];
}

【讨论】:

  • Henry,我试过这种方法,但它对我不起作用。我想要这种解决方案。
  • 好的,你想在同一页面上弹出一个窗口吗?
  • 没有。查看按钮将重定向其他页面。
  • 我编辑了答案,如果您为每个链接添加一个唯一的 ID,您将能够知道点击了哪个按钮。
  • if($action == 'send') { }此代码未通过此条件
猜你喜欢
  • 1970-01-01
  • 2021-03-08
  • 2013-05-09
  • 2013-07-26
  • 1970-01-01
  • 2014-05-19
  • 1970-01-01
  • 2014-12-28
  • 1970-01-01
相关资源
最近更新 更多