PHP and HTML Forms.
Introduction: This tutorial is going to be on how to use PHP to "interact" with HTML forms. Also, on how to "create" HTML forms, and the diffrent types of inputs. This tutorial is not a tutorial on how to use HTML tags, or how to learn basic PHP knowledge. You should have *basic* knowledge of HTML and PHP to understand this to the fullest extent.
Section 1 ~HTML Forms:
Using forms in HTML is a nice way that you can "interact" with visitors on your site. You can use them to collect information from your visitors, like in a member registration/login script.
A - First off, the HTML <form> tag -
The <form> tag must be opened <form [attributes]>, and closed </form>.
The HTML form tag is used to start a form. There are a few diffrent attributes that the <form> tag has.
1 - The "action" => The "action" attribute is used to specify which, or what, script or URL the form will submit its data to. I can be set to either a URL, or a script/program. More will be described next.
2 - The "method" attribute => The method can be set to either GET, or POST. If you want to know the "true" diffrence between these two ways to send data, use google.
-
GET. When you set the forms method equal to GET, when you submit the data, the data will be passed to the "action" in the actual URL.
Example:
Code:
<form action="index.php" method="GET">
When you submit that form, the data entered into the form's fields will be passed to "index.php" in name=value pairs.
More about form "inputs" will be explained later, but say you have:
Code:
<input type="text" name="username">
When you submit the form, it will send the data in the URL: index.php?username=value. Where username is the name of the "input", and "value" is the value that has been typed into that "input".
-
POST. When you set the forms method equal to POST, when you submit the data, the data will be not be passed to the "action" in the actual URL. Instead, the information is still passed to the "action", just the visitor doesn't see it, it is "hidden".
Example:
Code:
<form action="index.php" method="POST">
When you submit that form, the data is passed to the "action", just hidden.
Say you have:
Code:
<input type="text" name="username">
In your form. When you submit the form, it will send the data to the "action": index.php. You won't see the data in the URL though.
That is the best way I can describe the diffrence between GET and POST for "newbies". More seasoned "coders" might know actually the diffrence. I do, but if I explained it here, a lot of you would be confused. Anyways, to move on.
B - Second, the HTML <input> tag -
The <input> tag I would say is the most common HTML form tag, besides the <form> tag itself.
There are a lot more attributes to an <input> tag.
1 - The "name" attribute => The name attribute is used to "tell" whatever script that will "handle" the form when submitted, the specific <input> tag. The name is relied on "heavily" also when using PHP to "receive" submitted forms, I would also say. The name can be anythign you wish, (within reason

). Also, you can have as many <inputs> as you wish within <form> tags.
The name is set using:
Code:
<input name="whatever">
More will be discussed later on, on how to use this.
2 - The "value" attribute => The value attribute is used to automatically "add" data to that specific <input>. Example:
Code:
<input name="string" value="Hello World!">
That would "automatically" put the text "Hello World!" into the <input> tag, much like the visitor typed it. Except they wouldnt have. More will be discussed later on, on how to use this.
3 - The "type" attribute => The type attribute is used to determine which type of input the <input> will be. The input type can be:
-
Text => Makes an input where the user can type plain-text characters into the "box". (Shows a "box" that the visitor can type text into).
Example:
Code:
<input type="text" name="username">
-
Submit => Makes an input "button", that when "clicked", submits the form. When used, the <input>'s value attribute is what is "shown" on the button.
Example:
Code:
<input type="submit" value="Submit Form!">
-
Hidden => When used, it doesnt show anything in the users browser, except in the source code. It is used to pass information to scripts that the user wouldnt see and alter without looking at the source of the page. It is used the same way as "text" <input>'s, just not shown.
Example:
Code:
<input type="hidden" name="memberid" value="5412">
There are many many more attributes that can be used in <input> tags. Some, but not limited to, would be: size, maxlength, id, readonly, disabled, style. Sse Google if you want to learn about them.
There are many other HTML tags that can be used in forms. Some more are select, checkbox, radio button. Again, use Google if you want to learn about them. The same basis applies to them as <input> tags, on how to interact with them using PHP.
Here is an example of an HTML form:
Code:
<form method="post" action="login.php">
<input type="text" name="username">
<input type="text" name="password">
<input type="submit" value="Login Now!" name="submit">
</form>
By now, you should understand everything that is there, and be able to figure out what that will show on an HTML page.
Edit: Fixed the aligning.