PHP FORM HANDLING-rbgtech.blogspot.com
.To build a form, you must have at least the following elements:
a) An opening <form> and closing </form> tag
b) A submission type specifying either a Get or Post method
c) One or more input fields
d) The destination URL to which the form data is to be submitted
SIMPLE PHP FORM HANDLER
<?php // formtest.php
echo <<<_END
<html>
<head>
<title>Form Test</title>
</head>
<body>
<form method="post" action="formtest.php">
What is your name?
<input type="text" name="name">
<input type="submit">
</form>
</body>
</html>
_END;
?>
N/B:Rather than dropping in and out of PHP code, the echo <<<_END..._END con‐
struct is used whenever multiline HTML must be output.
UPDATED VERSION OF formtest.php
<?php // formtest2.php
if (isset($_POST['name'])) $name = $_POST['name'];
else $name = "(Not entered)";
echo <<<_END
<html>
<head>
<title>Form Test</title>
</head>
<body>
Your name is: $name<br>
<form method="post" action="formtest2.php">
What is your name?
<input type="text" name="name">
<input type="submit">
</form>
</body>
</html>
_END;
?>
.The only changes are a couple of lines at the start that check the $_POST associative
array for the field name having been submitted
.the $_POST associative array, which contains an element for each field in an HTML form
.The PHP isset function is used to test whether $_POST['name'] has been assigned a
value
<form method="post" action="calc.php"><pre>
Loan Amount <input type="text" name="principle">
Monthly Repayment <input type="text" name="monthly">
Number of Years <input type="text" name="years" value="25">
Interest Rate <input type="text" name="rate" value="6">
<input type="submit">
</pre></form>
THE INPUT TYPES
1)Text boxes
.It accepts a wide
range of alphanumeric text and other characters in a single-line box.
.The general for‐
mat of a text box input is as follows:
<input type="text" name="name" size="size" maxlength="length" value="value">
2)Text areas.When you need to accept input of more than a short line of text, use a text area. This
is similar to a text box, but, because it allows multiple lines, it has some different
attributes. Its general format looks like this:
<textarea name="name" cols="width" rows="height" wrap="type">
</textarea>
.To control the width and height, use the cols and rows attributes. Both use the char‐
acter spacing of the current font to determine the size of the area. If you omit these
values, a default input box will be created that will vary in dimensions depending on
the browser used, so you should always define them to be certain about how your
form will appear.
3)Checkboxes
.When you want to offer a number of different options to a user, from which he can
select one or more items, checkboxes are the way to go. Here is the format to use:
<
input type="checkbox" name="name" value="value" checked="checked">
.If you wish to offer a newsletter to your readers when submitting
a form, you might want to have the checkbox already checked as the default value:
Subscribe?
<input type="checkbox" name="news" checked="checked">
Offering multiple Checkboxes
Vanilla <input type="checkbox" name="ice" value="Vanilla">
Chocolate <input type="checkbox" name="ice" value="Chocolate">
Strawberry <input type="checkbox" name="ice" value="Strawberry">
4)Radio buttons
They are used when you want only a single value to be returned from a selection of
two or more options. All the buttons in a group must use the same name and,
because only a single value is returned, you do not have to pass an array.
For example, if your website offers a choice of delivery times for items purchased
from your store, you might use HTML like that.
8am-Noon<input type="radio" name="time" value="1">
Noon-4pm<input type="radio" name="time" value="2" checked="checked">
4pm-8pm<input type="radio" name="time" value="3">
<select>
.The < select> tag lets you create a drop-down list of options, offering either single or
multiple selections. It conforms to the following syntax:
<select name="name" size="size" multiple="multiple">
.The attribute size is the number of lines to display. Clicking on the display causes a
list to drop down, showing all the options. If you use the multiple attribute, a user
can select multiple options from the list by pressing the Ctrl key when clicking. So to
ask a user for his favorite vegetable from a choice of five, you might use HTML as in:
Vegetables
<select name="veg" size="1">
<option value="Peas">Peas</option>
<option value="Beans">Beans</option>
<option value="Carrots">Carrots</option>
<option value="Cabbage">Cabbage</option>
<option value="Broccoli">Broccoli</option>
</select>
1 Comments
wow
ReplyDelete