So how does a form look like? It's just plain HTML probably called index.html
- Code: Select all
<form method="get" action="search.php">
<input type="text" name="question" />
<input type="submit" name="submit" value="Search" />
</form>
Let's break this down into pieces to get a better understanding
- Code: Select all
<form method="get" action="search.php">
This is the opening part of the form. Anything between this and </form> will be treated as a form element. There are two attributes. Method says HOW the form data will be sent and Action says WHERE the data will be sent. If our current page is a HTML page we need to send it to a PHP script, thus search.php
Clicking the submit button we'll get something like
- Code: Select all
search.php?question=How+are+you&submit=Submit
Specifying the method attribute as GET anything we input into the form will be sent to the script for processing as a part of the URI delimited by "&".
If we were to say method was POST then the form details would be sent in a different way, without exposing the data in our URI. Thus, we'd only have the following in our URL.
- Code: Select all
search.php
The <input /> element is obviously an element that can accept data (text, files). It usually has three attributes
- type - what kind of input is this. "text" accepts text. "submit" is a button for submitting the form data. "hidden" contains text, but is invisible to the user. There are more types, these will come by later.
- name - we obviously need to give our input a name. E.g. date_of_birth or home_town is fine
- value - this let's us pre-enter a value for the input. We gave our submit button a "Submit" text
Next up: How to "read" the data sent in our PHP script.

