Skip to content
Advertisement

Getting value of clicked element in PHP

I have a page with element <a> acting like a <button>, in this form the elements works like a service plans, and i want to get a value when one of these buttons has been clicked.

Page1.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Page 1</title>
    <?php
    session_start();
    ?>
</head>
    <body>

        <section>
            <a href="page2.php" value='basic' name="plan">Choose Plan1</a>
            <br><br>
            <a href="page2.php" value='intermediary' name="plan">Choose Plan2</a>
            <br><br>
            <a href="page2.php" value='premium' name="plan">Choose Plan3</a>
        </section>

    </body>
</html>

Page2.php

<?php
session_start();
echo 'test';
echo $_POST['name'];
?>

I know that $_POST['name'] doesn’t works cuz don’t exist a input, but i don’t know how use something like this to get a value of button clicked and show this value.

The return expected for first button for example, is something like:

basic

I need this value to put in database after.

Someone can help me to get this values?

Advertisement

Answer

You may pass the value as a url parameter

        <section>
            <a href="page2.php?plan=basic" >Choose Plan1</a>
            <br><br>
            <a href="page2.php?plan=intermediary">Choose Plan2</a>
            <br><br>
            <a href="page2.php?plan=premium">Choose Plan3</a>
        </section>

And get it from $_GET in your php code

echo $_GET['plan'];

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement