Hello Folks,

How to rid GET METHOD from echoing the submitted webform's search button name in the destination url ?
My site search page's url looks like this:
http://localhost/Work/buzz/Templates/pagination_GET_METHOD_TEMPLATE.php
When I click the search button, I am taken to same page $_SERVER['PHP_SELF'];.
Taken to:
http://localhost/Work/buzz/Templates/pagination_GET_METHOD_TEMPLATE.php?search=&match=fuzzy&index=crawled_links_index&limit=1&web_search_button=

NOTE the last part of the destination url:
&web_search_button=
That is the search button's name.

<button type="submit" name="web_search_button" id="web_search_button" title="search the Web">Search!</button>

What to do so destination url looks like following when using GET METHOD ?
http://localhost/Work/buzz/Templates/pagination_GET_METHOD_TEMPLATE.php?search=&match=fuzzy&index=crawled_links_index&limit=1

Dani AI

Generated

A short, practical summary that builds on the thread: when a form uses GET the browser appends all "successful" controls that have a name to the query string — submit buttons are included if they have a name. As noted (and confirmed), omitting the button's name is a clean fix when the server does not need that input. The HTML spec and MDN explain which controls are sent as form data (WHATWG — constructing the form data set, MDN — Sending and retrieving form data).

Alternative approaches and quick examples:

  • Use POST when results should not be visible in the URL.
  • Use a small client-side handler to remove or clear the submit button name just before submit.
document.querySelector('form').addEventListener('submit', function () {
  var b = this.querySelector('button[type="submit"][name]');
  if (b) b.removeAttribute('name');
});
  • Or clean the query string server-side and redirect to a canonical URL (example uses a placeholder key):
<?php
$buttonKey = 'submit_button_name'; // set to the actual name used
if (!empty($_GET)) {
  $q = $_GET;
  unset($q[$buttonKey]);
  $q = array_filter($q, function($v){ return $v !== ''; });
  $base = strtok($_SERVER['REQUEST_URI'], '?');
  $url = $base . ($q ? '?' . http_build_query($q) : '');
  header('Location: ' . $url, true, 303);
  exit;
}
?>

Notes: keep the button name if the server must know which submit control was used; otherwise prefer omitting the name or using a hidden field populated by JavaScript. For PHP helpers see http_build_query.

Recommended Answers

All 4 Replies

@rproffitt

You know the answer, ofcourse.

When you submit a form, then every form field and button gets submitted to the form. When using the GET method, each and every one of those fields (including the button) becomes part of the query string. When using the POST method, each and every one of those fields (including the button) gets submitted in the body of the HTTP request.

I'm not sure if this will work, but what happens if you remove the name property from the button? e.g. remove name="web_search_button". I'm not sure if that will work but it's just a thought.

commented: It's kind of ok for me try for it. +0

You not sure it will work!
No programmers are really not sure of a basic thing like that ? You mean, you and all the progarmmers never even tried testing a basic thing like that when using GET and finding the button name got injected in the url and lengthened the url unnecessarily ?
And yet, again how come it did not even occur to me that it will work by removing the name attribute ? I can really talk!
What has got all us programmers blind ?
Anyway, it worked. Thanks!

It’s one of those things that makes sense to work but I’ve never tried it myself because I never had a need for it.

Typically, form buttons use POST and not GET, or at least that’s been my use case.

Glad it worked.

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.