October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

How to make a div or link-like control submit POST data to PHP

A plain anchor cannot submit a form as POST. Put the fields in one POST form, use a styled submit button, or carefully wire an existing div to requestSubmit(), then test a named action in PHP.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use one HTML form and make both controls submit that form. A normal <a href> navigates to a URL; it does not send the form fields as POST data. For a link-like appearance, use a real <button type="submit"> and style it with CSS. At the PHP endpoint, check the request method and, when necessary, a named action field.

The reliable pattern: one form, two submit controls

Browsers send form controls through a <form>. PHP then exposes URL-encoded or multipart form values in the $_POST associative array. Every value you need on the server must have a name attribute; an id by itself does not create a $_POST key.

<form method="post" action="process.php" id="taskForm">
  <input type="hidden" name="action" value="run">
  <input type="text" name="query">
  <button type="submit" class="link-like">Run</button>
  <button type="submit" class="tile-like">Run from tile</button>
</form>

Clicking either button submits the same fields to process.php with the POST method. A CSS class changes the appearance, not the control’s semantics.

Choose the PHP condition that matches what you need

Check that the request is POST

Use the request method when any POST to this endpoint should enter the handler:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $query = $_POST['query'] ?? '';
    // Validate $query, then process it.
}
?>

This distinguishes a POST request from a GET request, but it does not identify which form action was chosen.

Check a deliberate action field

When several actions share an endpoint, send a named marker and test its value:

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST'
    && isset($_POST['action'])
    && $_POST['action'] === 'run') {
    $query = $_POST['query'] ?? '';
    // Validate and process the requested action.
}
?>

A hidden field is predictable regardless of which submitter initiated the request. If you instead rely on a submit button’s name, remember that JavaScript submission methods may omit the clicked button’s name/value.

Make a submit button look like an anchor

This is the preferred solution when the visible result is a form submission rather than navigation:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
.link-like {
  border: 0;
  padding: 0;
  background: none;
  color: #06c;
  text-decoration: underline;
  font: inherit;
  cursor: pointer;
}

.link-like:focus-visible {
  outline: 2px solid currentColor;
  outline-offset: 3px;
}

The element remains keyboard-accessible, participates in native form validation, fires the form’s submit event, and has the expected button behavior for assistive technology.

If an existing div must trigger the form

Replace the div with a button if you can. If changing the markup is impossible, give the div button semantics, make it keyboard-focusable, and handle both keyboard activation keys:

<form method="post" action="process.php" id="taskForm">
  <input type="hidden" name="action" value="run">
  <input name="query">
</form>

<div id="runTile" role="button" tabindex="0" aria-label="Run query">
  Run query
</div>

<script>
const form = document.getElementById('taskForm');
const tile = document.getElementById('runTile');

function submitFromTile() {
  // requestSubmit() performs normal validation and dispatches submit.
  form.requestSubmit();
}

tile.addEventListener('click', submitFromTile);
tile.addEventListener('keydown', (event) => {
  if (event.key === 'Enter' || event.key === ' ') {
    event.preventDefault();
    submitFromTile();
  }
});
</script>

requestSubmit() behaves like a user pressing a submit button, including constraint validation and the form’s submit event. Calling form.submit() bypasses those steps and does not provide a submitter, so a button-specific name/value may not arrive in PHP. The hidden action field avoids that dependency.

When an anchor is the right control

Use an anchor when the operation is navigation:

<a href="results.php?query=example">View results</a>

That is a GET navigation. It does not submit the current form’s controls as POST data. If the server operation changes data or otherwise requires POST, use a submit button instead. Avoid href="javascript:..."; it produces misleading navigation semantics and creates avoidable keyboard and accessibility problems.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Common reasons isset($_POST[...]) is false

  • The clicked element is a plain anchor or a non-submitting div, so no form request was made.
  • The form uses method="get" or omits the method, so values are in the query string rather than $_POST.
  • The field has an id but no name.
  • JavaScript calls form.submit() and the code expects the clicked submit button’s name/value.
  • The PHP condition tests the wrong action value or assumes a field that the form does not send.
  • The request body is JSON. PHP does not populate ordinary $_POST entries from JSON; read the raw body with php://input and decode it instead.

A practical decision guide

Need Recommended control Server-side test
Send form fields to PHP <button type="submit"> $_SERVER['REQUEST_METHOD'] === 'POST'
Keep a link-like visual style Submit button styled with CSS Same as any form submit
Keep an unchangeable div Div with button semantics plus click and keyboard handlers calling requestSubmit() Check a hidden named action field
Navigate to another URL <a href="..."> Read GET parameters, not $_POST
Send JSON rather than form encoding JavaScript request with a JSON body Read and decode php://input

Validation and security still belong on the PHP side

Client-side controls and JavaScript improve interaction, but they do not prove that a request is valid. On the server, verify the method, require the expected action value, validate every submitted field, enforce authorization, and apply CSRF protection where the application needs it. Treat all values in $_POST as untrusted input.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.