如何使用HTML链接将变量从一个PHP文件传递到另一个PHP文件?
//DB CONNECTION
$sql = "SELECT `city`,`country` from infotab";
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()) {
echo $row["city"].$row["country"]"<a href='order.php'>order</a>"; }
Table output:
This code will select data. Additionally, there is reference to order.php
on every line. When the user clicks on reference( <a href>
clause), it opens order.php
and there I need to know which row the user selected to work with these data.
Change the code to:
while ($row = $result->fetch_assoc()) {
echo $row["city"] . $row["country"] . "<a href='order.php?city=" . $row["city"] . "&country=" . $row["country"] . "'>order</a>";
}
In order.php
you can then access these values by using the $_GET["city"]
and $_GET["country"]
variables which contain the values from your <a href>
link on the previous page. For example, running echo $_GET["city"];
will output the city name.
Edit: As @Rizier123 pointed out, using a unique ID might be more prone to errors in case your database contains more than one entry for the same city or country. You should consider introducing an ID in your table structure and then using that in the link to order.php
.