Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding URL parameters to a form PHP HTML

Tags:

html

php

webforms

Is it okay or correct to put a url get parameter in a form action?

<form method='get' action='index.php?do=search'>
  <input name='_search' type='text' value='What are you looking for?'>
  <button type='submit'> Search </button>
</form>

When I submit the form the URL is changed to:

index.php?_search=What are you looking for? (I've stripped %20)

I'd prefer the URL to read

index.php?do=search&_search=What are you looking for?

Would it be best to add a hidden field into the form

<input type='hidden' name='do' value='search' />
like image 390
JS1986 Avatar asked Apr 20 '11 02:04

JS1986


People also ask

How to submit form data in html?

The <input type="submit"> defines a button for submitting the form data to a form-handler. The form-handler is typically a file on the server with a script for processing input data. The form-handler is specified in the form's action attribute.

What is a URL parameter?

URL parameter is a way to pass information about a click through its URL. You can insert URL parameters into your URLs so that your URLs track information about a click. URL parameters are made of a key and a value separated by an equals sign (=) and joined by an ampersand (&).

What happens when you send data from a html form with GET method?

The GET method is the method used by the browser to ask the server to send back a given resource: "Hey server, I want to get this resource." In this case, the browser sends an empty body. Because the body is empty, if a form is sent using this method the data sent to the server is appended to the URL.


2 Answers

In my opinion you should add them as hidden fields. There is no point to try to pass params if you can do it via hidden form field

use that:

<input type='hidden' name='do' value='search' />
like image 135
Teodor Talov Avatar answered Sep 28 '22 00:09

Teodor Talov


A don't see any reason why you can't or shouldn't do it that way. My preferred method of handling it however would be:

<form method='get' action='index.php'>
    <input name='_search' type='text' value='What are you looking for?' />
    <submit name='do' value='Search'>
</form>

The name/value pair of do/search is passed through the button press, and if you want to create multiple actions on a form you can then have different values for each submit button, handling the form in multiple ways.

if ($_GET['do'] == "Search") {
 ... do Search ...
} else if ($_GET['do'] == "Foo") {
 ... do Foo ...
} else if ($_GET['do'] == "Bar") {
 ... do Bar ...
}

alternatively you can use a case construct:

switch($_GET['do']) {
    case "Search":
        ... do Search ...
    case "Foo":
        ... do Foo ...
        break;
    case "Bar":
        ... do Bar ...
        break;
} 

I normally use post myself, but I am sure get would work the same way. Hope that answers your question.

like image 26
Aaron Murray Avatar answered Sep 28 '22 01:09

Aaron Murray