Skip to content
Advertisement

How Can I Identify Which Linked Was Clicked With PHP?

I have connected to my database with PHP and selected a bunch of names with a query. Each name has an id value. I created a while loop that loops through the results and outputs something like this:

$name_id = 01 <a href='page.php'>name1</a> 
$name_id = 02 <a href='page.php'>name2</a>
$name_id = 03 <a href='page.php'>name3</a>

(id only displayed for explanation purposes)

At the linked page I want to run a query like SELECT * FROM database.table WHERE name_id = $name_id to select the attributes of the name clicked.

How can I access the name ID of the clicked link to later use that ID in a query in my PHP code?

Advertisement

Answer

Add a query string on to each URL.

<a href='page.php?name=1'>name1</a>
<a href='page.php?name=2'>name2</a>
<a href='page.php?name=3'>name3</a>

Then you can get the parameter in PHP using $_REQUEST['name']

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement