'Appending data to an anchor tag
i am new to PHP , i want to append data like query string and data i will recieve by $_GET[]
or $_POST[]
, but have facing a problem
i have done something like this <a href="?ref=login_detail?id=<?php echo $car_id; ?>">Hello</a>
this ?ref=login_detail
is a function which navigate me to another page. How can I pass $car_id
to anchor tag so that is available in another page?
Here is function
public function login_detail(){
include('display/car_detail.php');
}
Solution 1:[1]
First of all - only first parameter in URL should be prefixed will ?
, every other should be prefixed with &
.
So your URL should look like:
<a href="?ref=login_detail&id=<?php echo $car_id ?>">Hello</a>
Note the &
instead of second ?
.
After this, in the target script (which is probably index.php
) you can catch your $car_id
using:
var_dump($_GET['id']);
This should print the $car_id
you pass to it by clicking the link.
Going further, depending on your architecture you can change your method to accept some parameters like this:
class Obj
{
public function login_detail($id)
{
//do something with passed $id
echo $id;
}
}
(...)
$obj = new Obj();
$obj->login_detail($_GET['id']);
or just use it within the method itself:
class Obj
{
public function login_detail($id)
{
echo $_GET['id'];
}
}
$obj = new Obj();
$obj->login_detail();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | rr- |