Search code examples
phpgethref

Trouble getting a value from <a> link - PHP


My problem is that I need to save a value from the link as a variable which will then do something. Below is my main code for this problem.

<table>
     <? foreach ($csv as $row) : ?>
     <tr>
      <td><a href="?GoToProfile"><? echo $row[0]; ?></a></td> 
     </tr>
     <? endforeach; ?>
   </table>

Basically, it prints the first column from my array $csv. However I want to save the '$row[0]' for each link - depending on which one is clicked.

This happens here:

     <?php
       if (isset($_GET['GoToProfile'])) {

}
?>

This works. E.g. when something is clicked it prints something. But I cannot find a way to save the values from each link. Depending on which one is clicked. I have tried many different methods online, but none seem to work.

I have even tried:

<a href="?GoToProfile?id=<?php $row[0]; ?>"><? echo $row[0]; ?></a>

Any help would be greatly appreciated. Thanks!


Solution

  • Use an ampersand (&) instead of a question mark

     <a href="?GoToProfile&id=<?php $row[0]; ?>"><? echo $row[0]; ?></a>
    

    The ? indicates the beginning of the query string, which is the data sent on a GET request. In most cases it is a collection of name/value pairs, separated with & s.

    A simple example of a GET request

    http://example.com?first=1&second=fifty

    You would get the value of the parameters in PHP with $_GET

    $first = $_GET['first'];
    $second = $_GET['second'];
    

    To see what the server is receiving, you can use var_dump

    var_dump($_GET)