1

I have searched for this issue but all solution in javaScript related. I need to check a button is clicked or not. Button (<a href="something"></a>) is also link to other page. I have tried but my code is working for when anyone view that page. I want when anyone click on view button. My Code:

 $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>'; 

I have used datatable to show the data. If i don't use if condition then data is inserting when i page viewed. But i want when anyone click on View then data will be inserted. So i can i use if condition here?

Below Code is work for just view page:

    $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>'; 

Table:

enter image description here

Chonchol Mahmud
  • 2,717
  • 7
  • 39
  • 72
  • so why you don't use ajax? – m.elewa Dec 31 '18 at 08:12
  • @m.elewa i didn't with ajax much. – Chonchol Mahmud Dec 31 '18 at 08:15
  • 2
    I really don't get what you want to achieve, could you please elaborate this a bit clearer? – maio290 Dec 31 '18 at 08:21
  • Ajax would be your best option. – Joseph_J Dec 31 '18 at 08:22
  • @maio290 i want to insert some data(`ip & pc username`) into database when anyone click on view button(Look at the table picture). – Chonchol Mahmud Dec 31 '18 at 08:32
  • @Joseph_J can you give an example of ajax? – Chonchol Mahmud Dec 31 '18 at 08:34
  • Do you want to insert the value every time? Then why don't you make the View button an submit button for a form and send a post request out? That would be pretty pure in PHP and HTML. – maio290 Dec 31 '18 at 08:34
  • without JS, maybe what you can do is you can create another file like view.php and inside that just add table insertion code and redirect to the order page after inserted. the button link will be view.php – Naveen K Dec 31 '18 at 08:36
  • @maio290 No, i won't value every time. When PC username is different then i will insert these value. My code is work without click on view button(if i don't use if condition). When i just view the table page then its work. Now i want if anyone click any button then insert that button id, ip and pc username. – Chonchol Mahmud Dec 31 '18 at 08:43
  • Here is a link you can read that should explain some things for you. Read it and maybe watch some youtube tutorial and search around on SO. https://stackoverflow.com/questions/6009206/what-is-ajax-and-how-does-it-work It's really not that hard. Once you do it once you'll be ok. – Joseph_J Dec 31 '18 at 08:56
  • Here is another link. In this post there is a complete working script that will help you see the process. https://stackoverflow.com/questions/53955326/variable-remains-empty-when-using-ajax-in-sending-query-to-php-file/53955622#53955622 – Joseph_J Dec 31 '18 at 09:02
  • If you are avoiding Ajax, you could use a session variable. When button is clicked, append to a session variable. – Rotimi Dec 31 '18 at 09:06

1 Answers1

0

You could use a parameter on the button

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

In php script on page something, you use $_GET for the action.

$action = '';
if(isset($_GET['action'])) {
    $action = $_GET['action'];
}
if($action == 'send') {
    // the send buttons was clicked
    $id = $_GET['id'];
}
Henry
  • 1,242
  • 1
  • 12
  • 10