Search code examples
phpsessionposthrefisset

How to POST a href link with isset


Please help me, I wanted to do a post method using an anchor tag. Here is the sample code:

<form action="essay2.php" id="quiz" name="quiz" method="post">
  <ul>
    <li>
      <a href="essay2.php" name="submit-test">2</a>
    </li>
  </ul>
  <input type="text" name="test1">
</form>

And the PHP code:

<?php
session_start();

if (isset($_POST['submit-test'])) {
    $_SESSION['test1'] = $_POST['test1'];
    header("Location: essay2.php"]);
}
?>

Solution

  • To elaborate on my comment above...

    You can actually style a <button type="submit" name="submit-test">2</button> element to look exactly like a plain text link

    consider this approach

    button[name="submit-test"] {
      background: transparent;
      border: 0 none;
      color: blue;
      text-decoration: underline;
      font: inherit;
      cursor: pointer;
    }
    <form action="essay2.php" id="quiz" name="quiz" method="post">
      <ul>
        <li>
          <button name="submit-test" type="submit">2</button>
        </li>
      </ul>
      <input type="text" name="test1">
    </form>

    Results in the following sent when you click the "2"

    Network console