How to use the GET-method to get the id of the href that is clicked

Erwin picture Erwin · Dec 1, 2013 · Viewed 23.8k times · Source


I would like to know how I can use the GET-method in my .php file: forumdocument.php to get the ID of the href that is clicked in table.php
I hope this is the right way to begin it:

'<a class="formtitellink" href="forumdocument.php" id="$row['ID']">' . $row['Titel'] . '</a>' . "</td>";

table.php:

$query = "Select *
        from forum";
$resultaat = mysql_query($query, $connectie);
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Tijd</th>
<th>Gebruikersnaam</th>
<th>Titel</th>
</tr>";

while($row = mysql_fetch_array($resultaat))
  {
  $_SESSION['row'] = $row;
  echo "<tr>";
  echo "<td>" . $row['Tijd'] . "</td>";
  echo "<td>" . $row['Gebruikersnaam'] . "</td>";
  echo "<td>" . '<a class="formtitellink" href="forumdocument.php" id="$row['ID']">' . $row['Titel'] . '</a>' . "</td>";
  echo "</tr>";
  }
echo "</table>";

Answer

James picture James · Dec 1, 2013

A GET method is used for name value pairs in a url.

Example: www.test.com?var1=var1&var2=var2

Here if we use $_GET['var1'] the expected value would be var1.

Just change your links to reflect the desired variable

href="forumdocument.php?rowid=".$row['ID']."

Then you can use retrieve the id using something like this on forumdocument.php

$rowid = $_GET['rowid'];