Mitchell B Mitchell B - 2 years ago 57
PHP Question

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.

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

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:

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.

Answer Source

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

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download