Forms are designed to let you collect information from the people who use your website. This information is submitted to your web hosting server, where it is acted on by a server side program.
The html code required for this form is shown below. The html code
<form action="calcbmi.php" method="post"> Height (inches) <input type="text" name="hgt" size="2"> Weight (pounds) <input type="text" name="wgt" size="3"> <input type="submit" value="Submit Data"> </form>
When the submit button is clicked, the data is sent, and the "calcbmi.php" program called. This program is written in PHP. It accesses the data submitted, and calculates and displays the BMI.
<?php $wgt=$_POST[wgt]; $hgt=$_POST[hgt]; echo "Weight = ".$wgt." pounds<br>"; echo "Height = ".$hgt." inches<br>"; $bmi=$wgt/2.2/$hgt/$hgt*39.3*39.3; $out_bmi=sprintf('%0.1f', $bmi); echo "BMI = ".$out_bmi." kg/square meter"; ?>
The <form> tag controls the data submission process. It does this in two ways:
Those tags which are used to accept input include:
Note: Explanatory text and line breaks can be placed anywhere within the form to make it more intelligible.
The tag most commonly used for data input is the <input> tag. The <input> tag has no closing tag, so all information is specified by attributes within the tag.
There are at least ten different types of <input> tag. All accept different types of data. The type is, of course, specified by the 'type' attribute.
<input type="text"> |
<input type="password"> |
Types "button", "checkbox" and "radio" accept click data. Try clicking them.
<input type="button"> |
<input type="checkbox"> |
<input type="radio" name="x"> <input type="radio" name="x"> <input type="radio" name="x"> |
<input type="reset"> |
<input type="hidden"> |
<input type="file"> |
<input type="submit"> |
<input type="image" src="checkmarkanm.gif"> |
<input type="text" value="Type your answer here.">
<input type="password" value="BoJangles">
However, when used with types "button", "reset", and "submit", the value is used as a text label for the button.
<input type="button" value="Click here."> <br> <input type="reset" value="Reset Data"> <br> <input type="submit" value="Submit Data">
When used with types "radio" and "checkbox", the value sets a type for the data to be submitted. The default type is boolean.
The 'checked' attribute of the <input> tag can be used with types 'radio' and 'checkbox'. The syntax is:
<input type="checkbox" checked="checked" value="I">
or simply:
<input type="checkbox" checked value="I">
I have found so far, that the name, rather than the id attribute must be used. Still need to check whether this is a quirk, or dependent on the html version.
By using size="3" in the input field, you can limit the number of spaces allowed for input. maxlength limits the number of characters which can be input.
Here is a textarea. The number of rows and cols in the textarea are specified as attributes of the <textarea> tag. Some of the text has been entered. The user can add more text.
<textarea rows="10" cols="30"> The cat was playing in the garden. </textarea>
<form> Which fruit do you prefer? <select> <option>Apples <option selected>Bananas <option>Cherries </select> </form>The <select> tag can be used with the attribute "multiple". This allows the user to select more than one option. When multiple is used:
<form>
<select multiple size="3" name="fruits[]">
<option>Apples</option>
<option selected>Bananas </option>
<option selected>Cherries</option>
</select>
</form>
<form name="form1"> <input type="button" value="Whatzit" onClick="alert('Whatever')"> </form>The next example combines two fields for text input and a button to click on. The text inputs are named fname and lname. Since the form's name is NameIt, the names of the variables are:
<form name="NameIt"> What is your First Name?<input type="text" name="fname"> <br> What is your Last Name?<input type="text" name="lname"> <br> <input type="button" value="Click here." onClick="alert('Hello '+document.NameIt.fname.value+' ' +document.NameIt.lname.value)"> </form>
<form> I have a bike: <input type="checkbox" checked name="Bike"> <br> I have a car: <input type="checkbox" checked name="Car"> </form>
<form> Male: <input type="radio" checked name="Sex" value="male"> <br> Female: <input type="radio" name="Sex" value="female"> </form>
<fieldset> <legend> Body Mass Index Calculator: </legend> <form action="calcbmi.php" method="post"> Height (inches) <input type="text" name="hgt" size="2"> Weight (pounds) <input type="text" name="wgt" size="3"> </form> </fieldset>