How to pass parameters with a href

2018年5月11日 Off By ryosuke-hujisawa

index.php

<html lang="ja">
    <head>
        <meta charset="UTF-8" />
        <title>Pass value (parameter) to link destination</title>
        <link rel="stylesheet" href="./styles/main.css" />
    </head>
    <body>
        <h1>Original page</h1>
        <!--Pass id = 1 and name = value to the landing page-->
        <p><a href="next.php?id=1&name=value
">next
</a></p>
    </body>
</html>

next.php

<html lang="ja">
    <head>
        <meta charset="UTF-8" />
        <title>Receive a value (parameter) at the link destination</title>
        <link rel="stylesheet" href="./styles/main.css" />
    </head>
    <body>
        <h1>Landing page</h1>
        <?php

            // Receive a value with $ _GET function
            $id = $_GET['id'];      // Receive id and assign it to variable
            $name = $_GET['name'];  // Assign name to variable

            //output
            echo $id ."<br />\n"; // Output the variable id
            echo $name;             // Output variable name
        ?>
    </body>
</html>

result

1
value

reference

【PHP】リンク先で値(パラメータ)を受け取る