Can I use the html <a href></a> tag as a python form submit input type?

Dani AI

Generated

asked if an HTML <a> can act like a form submit; correctly pointed out that anchors are navigation controls (they produce GET-style requests) while forms are the proper way to send POST data. For actions that change server state (create/update/delete), use a form that submits with POST; for simple navigation or safe queries, a link is fine.

Two practical patterns that preserve correct semantics and graceful fallback:

  • Keep a real form and make the submit control look like a link (no JS required):
<form action="/items/delete" method="post" style="display:inline">
  <input type="hidden" name="id" value="123">
  <button type="submit" class="link-style">Delete</button>
</form>

.css:

.link-style { background:none; border:none; padding:0; color:blue; text-decoration:underline; cursor:pointer; font:inherit; }
  • Use an <a> with JavaScript to POST (progressive enhancement). Include a CSRF token and provide a non-JS fallback (e.g., a confirmation page):
<a href="/items/123/delete" id="del" data-csrf="TOKEN">Delete</a>

<script>
document.getElementById('del').addEventListener('click', function(e){
  e.preventDefault();
  fetch(this.href, { method:'POST', headers:{ 'X-CSRF-Token': this.dataset.csrf } })
    .then(r => { if (r.ok) location.reload(); });
});
</script>

Accessibility and security notes: prefer real <button> elements for actions (role and keyboard handling on anchors are extra work). Never use GET for destructive operations. Ensure server-side checks for request.method == 'POST' (Flask/Django) and validate CSRF tokens before making state changes.

In some instances, yes.

Clicking on a hypertext link sends a GET request. The following link and form send similar requests...

<a href="/somescript.php?id=123>Go get it!</a>

<form name="form1" method="GET" action="/somescript.php" >
<input name="id" type="text" value="123" />
<input type="submit" />
</form>

Hypertext links don't send POST requests, although there are ways to trigger a POST request with the help of JavaScript and AJAX. You probably wouldn't want to go down that road if you needed to ensure the behavior of the link remains consistent. What might happen if someone visits your site with JavaScript disabled in their client?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.