Html elements are so easy to learn. Even you are not major in computer science, even you are not a programmer, you can easily master them and use them to compose a complex webpage. But if you are going to learn website design by learning the syntax of html elements, you will be mistrayed.
I recall when I learned the <form> element, I wrote the following code:
<!--webpage1.php--> <html> <head><title>form</title></head> <body> <form action="webpage2.php"> <input name="input1"/> <input type="submit" name="input2"/> </form> </body> </html> <!--webpage2.php--> <html> <head><title>form</title></head> <body> <form action="webpage2.php"> <input name="input1" value="<?php echo $_GET["input1"]?>"/> <input type="submit" name="input2"/> </form> </body> </html>
You can see I write two separate web pages with almost the same content. In the first webpage(webpage1.php), I have a form that collect a user input. The form has an action that leads to the second page(webpage2.php) which collects the user input and echosĀ it in the input box. The reason I do so is that every book I read when talking about the <form> element, tells me to provide an action parameter. And I take it for granted that the action page should be another webpage different than the current page. So I use two different webpages that have the same form. But that is not the intention of the inventor of the <form> element.
The correct usage of <form> is as follows:
<!--webpage1.php--> <html> <head><title>form</title></head> <body> <form> <input name="input1" value="<?php if(!empty($_GET["input1"])) echo $_GET["input1"];?>"/> <input type="submit" name="input2"/> </form> </body> </html>
Here, we only use one web page(webpage1.php). The web page has a form without action. An form without action parameter does not affect the behavior of the submit button. When you click the submit button, it still fetches a web page to handle the form. But which webpage will it fetch? Without an action parameter, the current web page will be reloaded again, with the user input transferred back to the server, i.e., the current page will be refreshed. When webpage1.php is loaded the second time, the php code in the form will see $_GET[“input1”) is not empty and echo its value to the input box.
The original intention of the <form> element does not require you to provide an action parameter. The display of form and the collection of user input are assumed to be done by the same web page.