form processing with php
TRANSCRIPT
-
8/8/2019 Form Processing With PHP
1/23
Form Processing with PHP: Part 1
Fri, 06/30/2000 - 04:00 onaje
Form Processing with PHP: Part 1
Form Processing
Forms are usually used to gather information from visitors to your website. Once you've
gathered the information, it can be used in several ways. It can be sent via email for someone to
read, saved to database, or used to find data in the database that matches a specific criteria.
For example:
If you had a guest book that automatically save guests' comments into a database, you would
use a form on the guest book page to the comments into the database. To search your database
for what a specific guest said, you would can use a search form to pull individual guest
comments out of the database.
There are two parts to a form: the form fields that a visitor enters information into and the script
that process the information.
The typical sequence:
Display an empty form to the user
User fills out the form
User submits the form for processing when s/he is done filling out the form
Verify that the data entered into the form is valid and let the user know about an error if it occurs
Use the gathered data to perform an action
Form construction:
The form tag sets up the form properties.
-
8/8/2019 Form Processing With PHP
2/23
ENCTYPE: Encryption type for the data. The default MIME-type for form submission is
application/x-www-form-urlencoded. This MIME-type will take your text and will remove any
spaces and punctuation, replacing them with special character codes. The text/plain MIME-type
is often used with mailto form method to send data as plain text. The multipart/form-data MIME
type splits the form data into a number of separate parts, one for each form element, and then
sends the parts, each encoded in their own different way. This is used when uploading files with
a form.
METHOD: How the data gets treated (GET or POST), MAILTO
The GET method - the form data is combined into a string, and this string is added on to the end
of a url - for example: http://www.examplesite.com/form.php?first=firstname&last=lastname.
GET limits the amount of data that you can collect and submit to a script at one time.
The POST method groups the data together and sends it to the program by HTTP (HyperText
Transfer Protocol), you don't see what data is being sent. POST is recommended for PHP
because you can send more data
MAILTO: the mailto: method sends form data to a specific email address
ACTION: the location of script that will process the form.>
Form Elements:
Within a form tag, there are form elements. Each form element should have a name and a
value.
The name of the form element identifies the information being sent to a script. The value of the
form element is the actual data.
Input field Tags: Usually contains a short line of text.
< input type="text" name="a name" value=" a value A" size="#" maxlength="#" checked >
Input Attributes:
TYPE: Determines what kind of input field is shown in the form.
TEXT
PASSWORD
CHECKBOX
http://www.examplesite.com/form.php?first=firstname&last=lastnamehttp://www.examplesite.com/form.php?first=firstname&last=lastname -
8/8/2019 Form Processing With PHP
3/23
RADIO
HIDDEN
NAME: Sets the name of the form element, whose value will be assigned as the value of a
variable in the script. If the visitor does not fill out this field, and you did not give it a default
value, the value will be sent to the script as undefined.
VALUE: Sets the value of the form element. This can be pre-set to a defalut value or left blank
for the user to enter information* OPTIONAL *.
SIZE: Sets the size of the input ( Text and Password ) * OPTIONAL *.
MAXLENGTH: Sets the maximum number of characters typed in the input box ( Text and
Password ) * OPTIONAL *.
CHECKED: In Radio or Checkboxes, this pre-selectes an element before the user chooses it.
The different types of input type styles:
Text field: A text field allows numbers and characters to be entered.
first
Password: Same as a text field, however *'s appear instead of what the user types into the field.
< input type="password" name="second" value="" size="15" maxlength="15" >
Checkboxes: A group of boxes that can be selected. Usually denoted with squares, and a values
MUST be declared. ( Multiple boxes may be checked )
< input type="checkbox" name="third" value="pen">Pen
Paper
-
8/8/2019 Form Processing With PHP
4/23
Pen
Paper
Radio: The same as a checkbox, however if the 'names' are the same you can only select one of
the radio buttons. (Again a value MUST be declared)
< input type="radio" name="fourth" value="pencil" > Pencil
< input type="radio" name="fourth" value="crayon" checked > Crayon
Pencil
Crayon
Hidden: The hidden input filed, works exactly like the text field, however the user can't enter
anything into this box and it is not visible in a form, if you look at the source code of a page you
will be able to tell if a form has a hidden input field. It's best used to pass values that you don't
want the user to change. (A value MUST be declared)
< input type="hidden" name="fifth" value="Invisible Ink">You can't see me Invisible Ink
Select List boxes:
Select: The select list box lets you choose from a list of predefined choices
< select name="sixth" >
< option value="blue" > Blue ink
< option value="black" selected > Black ink
< option value="reds" > Red ink
< /select >
SELECTED pre-selects a choice.
SIZE Sets the size of the select list box * OPTIONAL *.
-
8/8/2019 Form Processing With PHP
5/23
Multiple Select: The multiple-select box lets you choose more than one choice from a list of
predefined choices
Hold down the 'Ctrl' key while clicking the mouse to select more than one choice.
Blue ink
Black ink
Red ink
Purple ink
Textarea: Allows a user to type a large amount of text into a text field. The 'wrap' attribute
determines how text in the textarea will behave.
Wrap attribute:
Physical: Press the 'enter' key for a new line
Virtual: New line is created when the sentece typed reaches the end of the column width
Hard: New line created by pressing the 'enter' key or when the end of the column width is
reached.
Size attributes:
Cols: Sets how many columns the textarea will be
Rows: Sets how many rows the textare will be
Reset / Submit: Creates the buttons for submitting and reseting the form.
Submit!
-
8/8/2019 Form Processing With PHP
6/23
Reset!
Forms can be implemented using either a single or multiple page design. The single page
technique utilizes a self-referring script to display and process the form. Multi-page forms take
the user through a series of pages to enter, confirm and submit the data.
The other important area to consider when thinking about form design is the reporting
mechanism for invalid data. Some people like to display the form with the input highlighting any
errors. This makes it simple for the user to find and fix problems in the data. Others prefer to
keep the page and application design as simple as possible by reporting errors on a separate
page. The user then needs to either go back to the original form using the back button on their
browser or they may be presented with a link to a page which will contain their entered data.
Putting it together:
When a form is submitted, all HTML variables are passed to a PHP script and assigned their
respective variable names. In our example, the input text field is named "UserName" so when
the form is submitted $UserName will hold the value. The variable $submit, that corresponds to
the input name="submit", will hold the value "click". It is only set if we click the submit button
and not when we directly access input.php with our browser.
PHP code for 'input.php'. It creates a form to allow users to enter their name.
PHP Code:
-
8/8/2019 Form Processing With PHP
7/23
";
?>
Remember to escape quotation marks, by putting a slash before them, if you're using 'echo' or
'print' to output HTML.
This section shows what will be seen, when the file is viewed in a HTML browser.
Web Browser Result:
Principio del formulario
EnterYourName
Final del formulario
Retrieving The Input When Submit is Clicked
Now we will add a simple condition to the beginning of our previous example. If the user clicked
the submit button then print out the name that was entered in the text field; otherwise, we
display the HTML form again.
Because the action in the form points to input.php our script will be called again when the user
clicks submit.
PHP Code:
-
8/8/2019 Form Processing With PHP
8/23
echo "
Enter Your Name
";
}
?>
See it in action. Get complete code.
http://onaje.com/code/input.phphttp://onaje.com/code/input.phpshttp://onaje.com/code/input.phpshttp://onaje.com/code/input.phphttp://onaje.com/code/input.phps -
8/8/2019 Form Processing With PHP
9/23
Using forms with PHP: part 2
Thu, 08/24/2000 - 04:00 onaje
Form Processing with PHP Part 2
Radio buttons, checkboxes, select lists
Radio Buttons:
data
VALUE: Sets the value of the form element.
RADIOSET links a group of radio buttons together, only one radio per set can be checked.
RADIOSET is alsothe name of the variable that will contain the value submitted by the form.
CHECKED is an optional attribute that makes a radio button active by default.
If the value is not set, "on" is the default value submitted. You can't tell which button in the set
was chosen.
How many days in a week ?:
5
6
7
What is the first day of the week?:
monday
sunday
saturday
PHP Code:
-
8/8/2019 Form Processing With PHP
10/23
See it in action. Get complete code.
Note: To pass multiple values, as in a "Checkbox or "Multiple Select List Box", put [] after the
variable name. This tells PHP to treat the form field as an array and each assignment of a value
to formfield[] adds an item to the array. The first item becomes $var[0], the next $var[1], etc.
Then on the receiving page, you simply do a count() on the list of variables that were selected.
Also see the PHP FAQ
http://onaje.com/code/radio.phphttp://onaje.com/code/radio.phpshttp://onaje.com/code/radio.phpshttp://onaje.com/code/radio.phphttp://onaje.com/code/radio.phps -
8/8/2019 Form Processing With PHP
11/23
Checkboxes:
Checkboxes are linked by their name attribute, similar to radio buttons. Unlike radio buttons
checkboxes can accept more than one answer per set.
VALUE: Sets the value of the form element.
BOXSET links a group of checkboxes together, more than one checkbox per set can be checked.
BOXSET is alsothe name of the variable that will contain the values submitted by the form.
CHECKED is an optional attribute that makes checkboxes active by default.
If the value is not set, "on" is the default value submitted. You can't tell which checkbox in the
set was choosen.
-
8/8/2019 Form Processing With PHP
12/23
Select List Boxes:
data
VALUE: Sets the value of the form element.
SIZE is the number of options from the select list that will be visible to the user
MULTIPLE is an optional attribute that allows a user to select more than one option from the
select list using the Ctrl or Command key.
SELECTED is an optional attribute that makes an option in the select list active by default.
If the size is larger than the number of options in the list, clicking in the empty space will
deselect all values.
1. Single Select:
How old are you?
How many years older is your brother/sister?
0
1
3
4
5
PHP Code:
See it in action. Get complete code.
Multiple Select:
http://onaje.com/code/single.phphttp://onaje.com/code/single.phpshttp://onaje.com/code/single.phpshttp://onaje.com/code/single.phphttp://onaje.com/code/single.phps -
8/8/2019 Form Processing With PHP
13/23
None
white
black
blue
brown
green
yellow
red
orange
purple
PHP Code:
See it in action. Get complete code.
http://onaje.com/code/multi.phphttp://onaje.com/code/multi.phpshttp://onaje.com/code/multi.phpshttp://onaje.com/code/multi.phphttp://onaje.com/code/multi.phps -
8/8/2019 Form Processing With PHP
14/23
Using forms with PHP: part 3
Thu, 09/28/2000 - 04:00 onaje
Form Processing with PHP: Part 3
How to populate form fields with previously submitted values.
When you see the scripts in action, take a look at the source code, especially what the values of
the form fields are set to before and after the scripts execute.
Input field:
To automatically have the value of an input field filled out, use the echo/print function to place
the value submitted into the value attribute of the field.
PHP Code:
What is your first name?
//Check to see if a value was submited then write it to the value
attribute of the input field
....
-
8/8/2019 Form Processing With PHP
15/23
}
?>
See it in action. Get complete code.
Select List Boxes:
To automatically have a select list display the option value that you selected when you
submitted the form, check to see if the variable is not empty and isn't the default value. If
there's a value for the variable, use the echo/print function to output an option with the
previously submitted value.
PHP Code:
A leap year occurs every
//Check to see if, the value submited is not - select number -
//then set the value of the option to contains the preselected value
1
http://onaje.com/code/populate1.phphttp://onaje.com/code/populate1.phpshttp://onaje.com/code/populate1.phpshttp://onaje.com/code/populate1.phpshttp://onaje.com/code/populate1.phphttp://onaje.com/code/populate1.phps -
8/8/2019 Form Processing With PHP
16/23
2
..... years.
....
See it in action. Get complete code
Check Boxes:
Place checkbox values into array using implode function" $cols = implode($colors, ",");
When you want to see what check boxes were checked, you use the explode function to get the
values from the array: $colarray=explode(",",$cols);
In the case where you are placing values into a database, you would insert the value of $cols
into a table column, then retrieve the value of the table column and assign it to a value, i.e.
$tablecolumn, then use the explode function - $cols=explode(",",$tablecolumn);
PHP Code:
http://onaje.com/code/populate2.phphttp://onaje.com/code/populate2.phpshttp://onaje.com/code/populate2.phpshttp://onaje.com/code/populate2.phphttp://onaje.com/code/populate2.phps -
8/8/2019 Form Processing With PHP
17/23
What color(s) do you like?
//Check to see if a boxset value is the array, if it is, make the
checkbox active
>Yellow
....
....
See it in action. Get complete code
Radio Buttons:
Are basically the same as checkboxes, but only one value can be checked if you give the same
name to set of radio buttons. You must give the buttons different names if you want to select
more than one.
PHP Code:
-
8/8/2019 Form Processing With PHP
18/23
//get the values of the radioset from an array
$vals=explode(",",$dinero);
?>
How much money would you like to earn per week?
//Check to see if a radioset value is the array, if it is, make the radio
button active
>100
....
....
See it in action. Get complete code
Textareas:
make sure there is no whitespace in your file because when echoing back your info in the textare
it may not appear correctly.
PHP Code:
http://onaje.com/code/populate5.phphttp://onaje.com/code/populate5.phpshttp://onaje.com/code/populate5.phpshttp://onaje.com/code/populate5.phphttp://onaje.com/code/populate5.phps -
8/8/2019 Form Processing With PHP
19/23
What is your favorite food to eat?
//Check to see if a value was submited then write it between the textarea
tags
}else{
?>
....
See it in action. Get complete code
http://onaje.com/code/populate4.phphttp://onaje.com/code/populate4.phpshttp://onaje.com/code/populate4.phpshttp://onaje.com/code/populate4.phphttp://onaje.com/code/populate4.phps -
8/8/2019 Form Processing With PHP
20/23
Checkbox In Form
I've created in a form:
-
8/8/2019 Form Processing With PHP
21/23
Form Processing with PHP: part 4
Thu, 01/18/2001 - 05:00 onaje
Form Validation
Validating a form
You can perform form validation before a form is submitted via JavaScript or after the form is
submitted via PHP.
I've actually already demonstrated simple validation using PHP in Form Processing with PHP:
Part 1, with the input.php script. After the form is submitted, the script checks to see whether a
variable does or does not exist.
PHP Code:
-
8/8/2019 Form Processing With PHP
22/23
You can also use regular expressions with the ereg() and eregi() functions to validate the values
submitted by a form. Regular expressions examine a string and search for patterns and
variations to determine whether they match the search criteria you set.
An example:
This regular expression tests true if the user has entered only characters, from a to z, in
lowercase or uppercase, and numbers from zero to nine. Also, checks whether the string is four
to twelve characters in length.
The ^ indicates the beginning of a string and the $ the end of the string.
if (!ereg("^[A-Za-z0-9]{4,12}$",$Username)): echo "Your username must be at least 4
character, but not more than 12 characters.";
PHPBuilder.com has a good tutorial demonstrationg how to use regular expressions called
Learning to Use Regular Expressions by Example.
You could also use JavaScript to do client-side validation of form values before the form is
submitted to a PHP script. Irt.org has a great tutorial - Addressing Form Field Validation with
Regular Expressions and JavaScript 1.2 that explains how to use JavaScript and regular
expressions to do form validation.
Here is an example:
JavaScript Code:
-
8/8/2019 Form Processing With PHP
23/23
// otherwise the string is valid
return true;
}
function isReady(form) {
if (isProper(form.firstname.value) == false) {
alert("Please enter your firstname");
form.firstname.focus();
return false;
}
return true;
}
//-->
See it in action. Get complete code.
http://onaje.com/code/jscriptcheck2.phphttp://onaje.com/code/jscriptcheck2.phpshttp://onaje.com/code/jscriptcheck2.phpshttp://onaje.com/code/jscriptcheck2.phpshttp://onaje.com/code/jscriptcheck2.phphttp://onaje.com/code/jscriptcheck2.phps