0

Imagine I have a simple form:

<form id="add" method="POST" action="/add/emp">
    <label for="name">Name: </label>
    <input type="text" id="name"> <br>
    <input type="submit" value="Save">
</form>

I expected that after clicking to the submit button a request http://localhost:8080/add/emp?name=_inputed_value_ will be sent, but there was just http://localhost:8080/add/emp without any parameters. Is it normal behavior and I need to add request-parameters with javaScript?

St.Antario
  • 23,179
  • 26
  • 96
  • 243
  • post doesn't add anything to your url textbox, if you want to achieve that you should use 'method="get" – Goos van den Bekerom Jan 06 '15 at 10:30
  • change method to `method="GET"` then you can see the parameters in querystring... – T J Jan 06 '15 at 10:31
  • @TJ But I need to save the data on the server. How to do it via post? – St.Antario Jan 06 '15 at 10:35
  • Don't let you misguide by the names. You can save data on the server with GET and you can read data with POST. The main thing that changes is the way the parameters are transmitted. With GET the parameters will be in the URL. With POST they will be in the request body. – Daniele Torino Jan 06 '15 at 10:45

4 Answers4

2

You're using the form method POST. Using GET would result in the desired behaviour.

What is the difference between POST and GET?

Community
  • 1
  • 1
gratz
  • 1,276
  • 3
  • 13
  • 31
2

Yes It is. When you use GET method the values are reflected in ur URL ie. You can see the values submitted.But It is not possible when you use POST method.Athough the form is getting submitted you cannot see the values in URL

Sameer Shaikh
  • 6,196
  • 2
  • 13
  • 31
1

The POST method of form submission as in your code, does not sumbit the form data through URL parameter. this method (POST) is more secure and is able to transfer larger data!

To aubmit (and see) your form data in the browser url, use method=GET!

To get the form data in your action file:

$var = $_GET['form_field_name']; // if the method is GET
$var = $_POST['form_field_name']; // if the method is POST
Afghan Dev
  • 6,691
  • 9
  • 42
  • 71
  • But I need to save data on the server, then I should use POST instead of GET. How to save the data with the POST request? – St.Antario Jan 06 '15 at 10:34
  • you can use any of the method.Saving data in database would require server side scripting language eg.php – Sameer Shaikh Jan 06 '15 at 10:35
  • @SameerShaikh I understand, but I need to pass the data in any way before. With post method the data won't be added to the request parameters. – St.Antario Jan 06 '15 at 10:40
0

GET vs. POST

Both GET and POST create an array (e.g. array( key => value, key2 => value2, key3 => value3, ...)). This array holds key/value pairs, where keys are the names of the form controls and values are the input data from the user.

Both GET and POST are treated as $_GET and $_POST. These are superglobals, which means that they are always accessible, regardless of scope - and you can access them from any function, class or file without having to do anything special.

$_GET is an array of variables passed to the current script via the URL parameters.

$_POST is an array of variables passed to the current script via the HTTP POST method.

When to use GET?

Information sent from a form with the GET method is visible to everyone (all variable names and values are displayed in the URL). GET also has limits on the amount of information to send. The limitation is about 2000 characters. However, because the variables are displayed in the URL, it is possible to bookmark the page. This can be useful in some cases.

GET may be used for sending non-sensitive data.

Note: GET should NEVER be used for sending passwords or other sensitive information!

When to use POST?

Information sent from a form with the POST method is invisible to others (all names/values are embedded within the body of the HTTP request) and has no limits on the amount of information to send.

Moreover POST supports advanced functionality such as support for multi-part binary input while uploading files to server.

However, because the variables are not displayed in the URL, it is not possible to bookmark the page.
Gaurav Dave
  • 5,038
  • 9
  • 21
  • 37