How to pass parameters with a href

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】リンク先で値(パラメータ)を受け取る

藤沢瞭介(Ryosuke Hujisawa)
  • りょすけと申します。18歳からプログラミングをはじめ、今はフロントエンドでReactを書いたり、AIの勉強を頑張っています。off.tokyoでは、ハイテクやガジェット、それからプログラミングに関する情報まで、エンジニアに役立つ情報を日々発信しています!

未整理記事