简体   繁体   中英

How to pass variables from one PHP file to another using HTML links?

    //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:

http://imgur.com/jH2VzBf

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM