forms | action and method

Hey, i have a couple of questions about the action and method attributes for forms- 1) With action, what necessarily does sending data to other html/pfp files etc do? I can imagine it's so you can use it with js that's hooked up to the page it's sending to to either use it in a query or display it on the page. This makes sense to me since i imagine you'd have to use a database or something similar to retrieve data between pages without this functionality. let me know if i'm wrong though please. 2) With the GET method, what's the function of appending the data to the url? What does it allow you to do? 3) When would you use GET vs POST? I assume POST when sending the form data somewhere with action. With GET, the answer depends on the answer to question 2) but it would confuse me if you had a GET method with a link in action as where is the data going at that point if you're getting data from somewhere else? I'd appreciate any help clearing this up, thank you in advance.
21 Replies
Chooโ™š๐•‚๐•š๐•Ÿ๐•˜
1) This would not be a question if you had learned web development before single page apps were invented. There was a time when the common practice was to send form data to the back end, and a totally new page would be loaded in the browser that was fully rendered based on the form data that the back end received. This is still possible but much less common. 2) Having the query string in the URL makes it possible to bookmark. That allows you to revisit the exact same query again. This could be useful for something like searching for flights, concert tickets, etc. You don't need to put in all the same search parameters again. 3) Use POST if the data needs to be secure and/or if there is a lot of data. There are limits to the total length that a URL can have, so a GET should not be used if a signficant amount of data is being transmitted. Because the query string is visible both in bookmarks and in browser history, GET should also be avoided if the data has to be secure. There is one other important reason for choosing between GET or POST. There is something known as a REST API. This standard states that fetching data should be done with a GET request and submitting data for storage should be done with POST.
แผ”ฯฯ‰ฯ‚
with POST or PUT, depending on the data for example, imagine you have a service to mail big files you can use POST to create the basic email, with the contents and who it is for then, with the id of the email, you can PUT a file later on, before sending a POST saying that the email is ready to be sent or, for example, a mod for a game
snxxwyy
snxxwyyOPโ€ข6d ago
1) oh okay, that makes sense then, how would sending it there work? Is it like a private url you put in action? 2) ah okay. if you had a search flight system like in your example, if you revisited a link with parameters in, wouldnโ€™t you have to click the โ€˜searchโ€™ or submit button for it to pull up all the search results again? Or do the parameters existing tell the browser that itโ€™s been clicked and it assumes it has? Maybe testing if the form is submitted in js that will run? 3) so if the data need to be secure, youโ€™d use POST to send it it to the backend to put into letโ€™s say a database? Like login details? And if itโ€™s GET you can send it to a backend to pull the values and maybe compare them to something? Like for search results? Ahh okay I see, thanks for the example A quick follow up question, is the way you handle the form data on the web page it sends to what I referenced in the first question? Or something else? If I sent data to example.com, could I then retrieve it using js on that site? Is it in itโ€™s storage or something?
Jochem
Jochemโ€ข6d ago
you can with method="GET", but in general submitted forms are used to process data in the backend, where you would use your backend language to then process the submitted data
snxxwyy
snxxwyyOPโ€ข6d ago
Oh so only js for local ones since it adds to the url? but how would you send it to a backend? What would you put as the action value?
Jochem
Jochemโ€ข6d ago
all the values would send it to the backend too, a form submission by default causes an http request to be fired
snxxwyy
snxxwyyOPโ€ข6d ago
Oh so you can retrieve the http request and its values via your backend language?
Jochem
Jochemโ€ข6d ago
That's always part of the global state your backend has access to yes
snxxwyy
snxxwyyOPโ€ข6d ago
Ahh okay that makes sense now, so if youโ€™re sending it to another website/url thatโ€™s how it accesses it since you can pull the http request data Thank you for clearing that up
Jochem
Jochemโ€ข6d ago
another or your own, but yes
snxxwyy
snxxwyyOPโ€ข6d ago
And then I assume itโ€™s the same with GET too? You use the http request data to do things? Or is that more js orientated?
Jochem
Jochemโ€ข6d ago
yeah, GET parameters are also accessible in the backend
Chooโ™š๐•‚๐•š๐•Ÿ๐•˜
1) Form data gets appended to the url automatically. You don't need to add that to the action. The action is actually the base URL and the query parameters get automatically added to the end of it. 2) The whole point is you don't need to search again. When you load the URL, you just get the results. Here is an example from the Google search for "form action". https://www.google.com/search?q=form+action&oq=form+action Notice that the search terms are in the URL. You can just load that URL and get the search results. You do not need to click on search. 3) Something like that, but the GET results don't have to be compared to anything. It is just for getting data. For example, if I do a Google search, a GET request is sent with my search terms and the results are returned to me.
snxxwyy
snxxwyyOPโ€ข6d ago
ah okay i see, thank you. for 2), is that a built in functionality that it submits the form again when you enter a url with parameters or do you have to make that yourself?
แผ”ฯฯ‰ฯ‚
it doesn't submit the form
Chooโ™š๐•‚๐•š๐•Ÿ๐•˜
It does not submit the form again. When a form is submitted using GET, it's just a request to load the page.
แผ”ฯฯ‰ฯ‚
that's the whole point of using GET you can sent the url parameters without having to submit the form again all the information was already sent to the server, as if you had submitted the form
snxxwyy
snxxwyyOPโ€ข6d ago
oh okay i see, so whatever parameters are in the url are just sent to the server?
แผ”ฯฯ‰ฯ‚
yes, like a normal request
snxxwyy
snxxwyyOPโ€ข6d ago
ah okay i see. thank you both
แผ”ฯฯ‰ฯ‚
you're welcome
Want results from more Discord servers?
Add your server