This might not be specific to PHP (because I’ve seen ‘action’ and ‘method’ used in javascript, too), but I encountered questions early in learning PHP. I wanted to be sure I understood what a tag like really does. I searched first to learn the definition of the attribute ‘action’ and found this: “The action attribute in PHP forms specifies the URL where the form data should be sent when the form is submitted. It determines which server-side script will process the submitted data. GeeksforGeeks W3Schools”
This confused me because I don’t call “foo-php” a URL but a file name.
This leads to my question. Assuming that the statement quoted is correct (the action attribute specifies a URL), what forms can a URL take? So, I did another search and found this: “A URL can take various forms, including web addresses for web pages (like http://www.example.com/index.html), file transfer links (like ftp://example.com/file.txt), and email links (like mailto:user@example.com). It typically consists of a scheme, authority, path, query, and fragment components. Wikipedia Mozilla”.
I thought URLs took http or ftp prefixes and followed with appropriate syntax. I don’t understand references to scheme, authority, query, and fragment components. My understanding of path is a form like ‘C:\\folder\filename.txt’, with the possibility that some parts might be presumed so that ‘folder\filename.txt’ sometimes suffices.
Can someone give me an education on the uniform resource locator (URL) or point me to a good wiki?
I’m not sure of a good document resource for URL, sorry. I looked around a bit and nothing got my juices flowing, so I won’t try.
I think the bit you’re missing though, regarding the html <form> action is that it may be specified as an absolute URL, which you acknowledge, or a relative URL. A relative URL is taken to be an offset from the current URL (the location of your current html page) or the html <base> if one exists.
All that to say that if you reference <form action="foo.php">, your form data will be sent to a file called foo.php that sits in the same directory as wherever your form html (or the php file that spit out the form) sits. There are ways to force the reference of foo.php to be in another directory (up one level, top of the tree, etc) if that’s of interest.
As with <img src="blah.jpg"> attributes, most URL references in your site probably should be relative, at least in the sense that you don’t need or probably want to keep repeating http://www.mydomain.com in every reference. Imagine deciding to change your domain name and needing to update every one of these, when leaving them off and letting the whole site be relative to “wherever index.php was found” is usually just fine.
If no URL rewriting is being used on the web server, then the path (folder/filename) portion of a URL does correspond to the path (folder/filename) within the web server’s document root folder.
BTW - when you change your code to have the form processing code and the form on the same page, the action attribute in the form tag can be removed. When there is no action attribute, this is the same as a relative URL to the current page, and the browser will submit the form to the exact same URL of the page the form is on.
I had never been down the URL structure and syntax rabbit hole, but I learned a lot.
I found the schematic in the attached ss. After parsing each element carefully, I have a much better understanding of URLs and how they can be used. New to me in this was the use of IPv4 and IPv6 addresses and port numbers. I’ve not had the need to use either (yet?) but found it interesting.