PHP and HTML Forms by Diya Yang


    What are forms

            Forms are user inputs that can be stored using the form tag and are often used for inputs such as username and passwords.

    What do they do

            The form tags can be used to store informations in to php file and can be used to call back other information, such as when you input a certain word in to a submit file, it will return something based on your input.

    How to use them

            A form can be created using "form" and "/form" tags(replace " with < and >), when you write a form, it have a few parts in it, as shown in the example below:

    <?php
    	echo '
    	<form action="test.php" method="get">
    	Name: <input type="text" name="name">
    	<input type="submit">
    	</form>';
    
    	if (isset($_GET["name"])) {
    		echo $_GET["name"];
    	}
    ?>	
    		

    As you can tell, this is a php code, and the form tag contain action and method:
  • Action shows where the form will take place. Usually if you write a form, it would link back to the same php file because you want the action to take place on the same page you started. However, by changing where the action php link to, you can choose which php page you want the form to take place in.

  • Method differentiates the security of the information, there are two type: Post and Get. They do the same thing except post does not show the information in the URL, while Get does. As seen below, When using the get method, the information is shown in the URL.

  • Get Method Example

    Different kind of forms

            There are many types of forms especially in HTML, and they are shown using < input type = "type of input here" name = "description"> where input type changes what the information in putted will look like and name serve as a description of how the form will be used for. The type of forms are: textfield, password, radio buttons, check boxes, and submit buttons. Most of them are self explanatory, radio buttons are like this:

    Just having the option isn't enough, in order to actually use it, you must call the information and command the computer to read the code in a specific way using "echo" command in php.

    PHP and HTML form work together and are very similar

            As shown above, PHP and HTML forms are very similar, but they use the form tag differently. While HTML file only use the form tag to show where it is, PHP actually specify what the form calls and the way the information is transferred.

    Example of how they work


    Result:

This page was made through the collaborative efforts of the 2012-2013 Shodor Apprentices. We hope that this tutorial will aid you in your programming endeavors and help you to learn PHP.

© Shodor Educational Foundation 2013