Concrete Facts, Variables, & Slab Installation Procedures Variables
9/4/2015CS346 PHP1 CHAPTER 2 Using Variables. 9/4/2015CS346 PHP2 Objectives How to store and access...
-
Upload
allan-wright -
Category
Documents
-
view
216 -
download
2
Transcript of 9/4/2015CS346 PHP1 CHAPTER 2 Using Variables. 9/4/2015CS346 PHP2 Objectives How to store and access...
04/19/2304/19/23 CS346 PHPCS346 PHP 11
CHAPTER 2Using Variables
04/19/2304/19/23 CS346 PHPCS346 PHP 22
Objectives Objectives
How to store and access data in PHP How to store and access data in PHP variablesvariables
How to create and manipulate numeric and How to create and manipulate numeric and string variablesstring variables
How to create HTML input formsHow to create HTML input forms How to pass data from HTML forms to PHP How to pass data from HTML forms to PHP
scriptsscripts
04/19/2304/19/23 CS346 PHPCS346 PHP 33
Using PHP VariablesUsing PHP Variables
All variables are preceded by the $ special All variables are preceded by the $ special symbolsymbol
$cost = 4.25; $months = 12;
Name of variable Variables new value
04/19/2304/19/23 CS346 PHPCS346 PHP 44
Selecting Variable NamesSelecting Variable Names Variable name in PHP may contain any set of Variable name in PHP may contain any set of
characters except:characters except: The first character must be a dollar sign ($)The first character must be a dollar sign ($) The second character must be a letter or an The second character must be a letter or an
underscore character (_)underscore character (_) The other characters can be letter, digit, underscore or The other characters can be letter, digit, underscore or
ASCII character from 127 to 255ASCII character from 127 to 255
Note: Select descriptive variable names. For Note: Select descriptive variable names. For example $counter is more descriptive than $c or example $counter is more descriptive than $c or $ctr$ctr
04/19/2304/19/23 CS346 PHPCS346 PHP 55
Combining Variables and the Combining Variables and the print print StatementStatement
To print out the value of $x, write the To print out the value of $x, write the following PHP statement:following PHP statement:
print ("$x");print ("$x");
The following code will output “Bryant is 6 The following code will output “Bryant is 6 years old”.years old”.$age=6;$age=6;
print ("Bryant is $age years old.");print ("Bryant is $age years old.");
04/19/2304/19/23 CS346 PHPCS346 PHP 66
A Full Example ...A Full Example ...1. <html>1. <html>
2. <head> <title>Variable Example </title> </head>2. <head> <title>Variable Example </title> </head>
3. <body>3. <body>
4. <?php4. <?php
5. $first_num = 12;5. $first_num = 12;
6. $second_num = 356;6. $second_num = 356;
7. $temp = $first_num;7. $temp = $first_num;
8. $first_num = $second_num;8. $first_num = $second_num;
9. $second_num = $temp;9. $second_num = $temp;
10. print ("first_num= $first_num <br>10. print ("first_num= $first_num <br>
second_num=$second_num");second_num=$second_num");
11. ?> </body> </html>11. ?> </body> </html>
04/19/2304/19/23 CS346 PHPCS346 PHP 77
A Full Example ...A Full Example ...
The previous code can be executed at The previous code can be executed at http://localhost/mod2/firstnum2-1.phphttp://localhost/mod2/firstnum2-1.php
04/19/2304/19/23 CS346 PHPCS346 PHP 88
Using Arithmetic OperatorsUsing Arithmetic Operators
Use Use operators operators + for addition and – for + for addition and – for subtraction to build mathematical expressions.subtraction to build mathematical expressions.
For exampleFor example<?php<?php
$apples = 12;$apples = 12;
$oranges = 14;$oranges = 14;
$total_fruit = $apples + $oranges;$total_fruit = $apples + $oranges;
print ("The total number of fruit is $total_fruit");print ("The total number of fruit is $total_fruit");
?>?>
These PHP statements would output “The total These PHP statements would output “The total number of fruit is 26.”number of fruit is 26.”
04/19/2304/19/23 CS346 PHPCS346 PHP 99
Common PHP Numeric OperatorsCommon PHP Numeric Operators
Table 2.1 Common PHP Numeric Operators
Operator Effect Example Result
+ Addition $x = 2 + 2; $x is assigned 4.
- Subtraction $y = 3; $y = $y – 1;
$y is assigned 2.
/ Division $y = 14 / 2;
$y is assigned 7.
* Multiplication $z = 4; $y = $z * 4;
$y is assigned 16.
% Remainder $y = 14 % 3; $y is assigned 2.
04/19/2304/19/23 CS346 PHPCS346 PHP 1010
A Full ExampleA Full Example1. <html>1. <html>
2. <head> <title>Variable Example </title> </head>2. <head> <title>Variable Example </title> </head>
3. <body>3. <body>
4. <?php4. <?php
5. $columns = 20;5. $columns = 20;
6. $rows = 12;6. $rows = 12;
7. $total_seats = $rows * $columns;7. $total_seats = $rows * $columns;
8.8.
9. $ticket_cost = 3.75;9. $ticket_cost = 3.75;
10. $total_revenue = $total_seats * $ticket_cost;10. $total_revenue = $total_seats * $ticket_cost;
11.11.
12. $building_cost = 300;12. $building_cost = 300;
13. $profit = $total_revenue - $building_cost;13. $profit = $total_revenue - $building_cost;
14.14.
15. print ("Total Seats are $total_seats <br>");15. print ("Total Seats are $total_seats <br>");
16. print ("Total Revenue is $total_revenue <br>");16. print ("Total Revenue is $total_revenue <br>");
17. print ("Total Profit is $profit");17. print ("Total Profit is $profit");
18. ?> </body> </html>18. ?> </body> </html>
04/19/2304/19/23 CS346 PHPCS346 PHP 1111
A Full Example ...A Full Example ...
The previous code can be executed at The previous code can be executed at http://localhost/mod2/numops2-2.phphttp://localhost/mod2/numops2-2.php
04/19/2304/19/23 CS346 PHPCS346 PHP 1212
WARNING: Using Variables WARNING: Using Variables with Undefined Valueswith Undefined Values
• Undefined variables have no value (called a null value)• PHP may not generate an error for a variable with a null value and may complete the expression evaluation.
•Error message turned off – see php.ini • For example, the following PHP script will output x= y=4.C:\wamp\bin\php\php5.3.0\php.ini<?php$y = 3;$y=$y + $x + 1; // $x has a null valueprint ("x=$x y=$y");?>
04/19/2304/19/23 CS346 PHPCS346 PHP 1313
WARNING: Using Variables with WARNING: Using Variables with Undefined ValuesUndefined Values
SimillarlySimillarly http://localhost/ch29/data.phphttp://localhost/ch29/data.php http://cs346.cs.uwosh.edu/huen/ch29/data.phphttp://cs346.cs.uwosh.edu/huen/ch29/data.php See textSee text http://cs346.cs.uwosh.edu/huen/ch29/data.php.txthttp://cs346.cs.uwosh.edu/huen/ch29/data.php.txt
04/19/2304/19/23 CS346 PHPCS346 PHP 1414
Shortcut: Automatic Increment/Decrement Shortcut: Automatic Increment/Decrement OperatorsOperators
Increment or decrement a value by 1: ++ and --Increment or decrement a value by 1: ++ and --<?php<?php $x=0; $y=2;$x=0; $y=2; print “x=“;print “x=“; print ++$x;print ++$x; --$y;--$y; print “ and now y=$y”;print “ and now y=$y”;?>?>
WHH WHH
04/19/2304/19/23 CS346 PHPCS346 PHP 1515
Writing ExpressionsWriting Expressions
Operator precedence rulesOperator precedence rules define the define the order in which the operators are order in which the operators are evaluated. For example,evaluated. For example,
$x = 5 + 2 * 6;$x = 5 + 2 * 6;
Since multiplication evaluated before Since multiplication evaluated before addition operations, this expression addition operations, this expression evaluates to 17. evaluates to 17.
04/19/2304/19/23 CS346 PHPCS346 PHP 1616
PHP Precedence RulesPHP Precedence Rules
PHP follows the precedence rules:PHP follows the precedence rules: First it evaluates operators within First it evaluates operators within
parenthesesparentheses Next it evaluates multiplication and division Next it evaluates multiplication and division
operators.operators. Finally it evaluates addition and subtraction Finally it evaluates addition and subtraction
operators.operators.
04/19/2304/19/23 CS346 PHPCS346 PHP 1717
PHP Precedence RulesPHP Precedence Rules
For example, the first 2 statements For example, the first 2 statements evaluate to 80 while the last to 180.evaluate to 80 while the last to 180. $x = 100 - 10 * 2;$x = 100 - 10 * 2; $y = 100 - (10 * 2);$y = 100 - (10 * 2); $z = (100 - 10) * 2;$z = (100 - 10) * 2;
04/19/2304/19/23 CS346 PHPCS346 PHP 1818
A Full ExampleA Full Example1. <html>1. <html>
2. <head> <title>Expression Example </title> 2. <head> <title>Expression Example </title> </head></head>
3. <body>3. <body>
4. <?php4. <?php
5. $grade1 = 50;5. $grade1 = 50;
6. $grade2 = 100;6. $grade2 = 100;
7. $grade3 = 75;7. $grade3 = 75;
8. $average = ($grade1 + $grade2 + $grade3) / 3;8. $average = ($grade1 + $grade2 + $grade3) / 3;
9. print ("The average is $average");9. print ("The average is $average");
10. ?> </body> </html>10. ?> </body> </html>
04/19/2304/19/23 CS346 PHPCS346 PHP 1919
A Full Example ...A Full Example ...
The previous code can be executed at The previous code can be executed at http://localhost/mod2/average2-3.phphttp://localhost/mod2/average2-3.php
04/19/2304/19/23 CS346 PHPCS346 PHP 2020
Working with PHP String VariablesWorking with PHP String Variables
Character strings are used in scripts to hold data Character strings are used in scripts to hold data such as customer names, addresses, product such as customer names, addresses, product names, and descriptions. names, and descriptions.
Consider the following example.Consider the following example. $name="Christopher";$name="Christopher"; $preference="Milk Shake";$preference="Milk Shake";
$name$name is assigned “Christopher” and the variable is assigned “Christopher” and the variable $preference$preference is assigned “Milk Shake”. is assigned “Milk Shake”.
04/19/2304/19/23 CS346 PHPCS346 PHP 2121
WARNING: Be Careful Not to WARNING: Be Careful Not to Mix Variable TypesMix Variable Types
Be careful not to mix string and numeric variable types. Be careful not to mix string and numeric variable types.
For example, you might expect the following For example, you might expect the following statements to generate an error message, but they will statements to generate an error message, but they will not. Instead, they will output “not. Instead, they will output “y=1y=1”.”.<?php<?php
$x ="banana";$x ="banana";
$sum = 1 + $x;$sum = 1 + $x;
print ("y=$sum");print ("y=$sum");
?>?>
04/19/2304/19/23 CS346 PHPCS346 PHP 2222
Using the Concatenate Using the Concatenate OperatorOperator
The concatenate operator combines two separate The concatenate operator combines two separate string variables into one. string variables into one.
For example,For example, $fullname = $firstname . $lastname;$fullname = $firstname . $lastname;
$fullname$fullname will receive the string values of will receive the string values of $firstname$firstname and and $lastname$lastname connected together. connected together.
For example,For example, $firstname = "John";$firstname = "John";
$lastname = "Smith";$lastname = "Smith";
$fullname = $firstname . $lastname;$fullname = $firstname . $lastname;
print ("Fullname=$fullname");print ("Fullname=$fullname");
04/19/2304/19/23 CS346 PHPCS346 PHP 2323
TIP; TIP; An Easier Way to An Easier Way to Concatenate StringsConcatenate Strings
You can also use double quotation marks to You can also use double quotation marks to create concatenation directlycreate concatenation directly
For example,For example, • $Fullname2 = "$FirstName $LastName";$Fullname2 = "$FirstName $LastName";• This statement has the same effect asThis statement has the same effect as• $Fullname2 = $FirstName . " " . $Fullname2 = $FirstName . " " . $LastName;$LastName;
04/19/2304/19/23 CS346 PHPCS346 PHP 2424
The strlen() FunctionThe strlen() Function The strlen() function returns the The strlen() function returns the length of its string argumentlength of its string argument
$len = strlen($name);
Variable or value to work with
Name of functionReceives the number of
characters in $name
04/19/2304/19/23 CS346 PHPCS346 PHP 2525
The strlen() Function ExampleThe strlen() Function Example
<?php <?php $comments = "Good Job";$comments = "Good Job"; $len = strlen($comments);$len = strlen($comments); print ("Length=$len");print ("Length=$len");
?>?>This PHP script would output “Length=8”.
04/19/2304/19/23 CS346 PHPCS346 PHP 2626
The trim() FunctionThe trim() Function
This function removes any blank characters This function removes any blank characters from the beginning and end of a string. For from the beginning and end of a string. For example, consider the following script:example, consider the following script:
<?php<?php $in_name = " Joe Jackson ";$in_name = " Joe Jackson "; $name = trim($in_name);$name = trim($in_name); print ("name=$name$name");print ("name=$name$name"); ?>?>
This script will output This script will output ““name=Joe JacksonJoe Jackson”name=Joe JacksonJoe Jackson”
04/19/2304/19/23 CS346 PHPCS346 PHP 2727
strtolower() and strtoupper()strtolower() and strtoupper() Return the input string in all uppercase or all Return the input string in all uppercase or all
lowercase letters, respectively.lowercase letters, respectively. For example,For example,
<?php<?php
$inquote = "Now Is The Time";$inquote = "Now Is The Time";
$lower = strtolower($inquote);$lower = strtolower($inquote);
$upper = strtoupper($inquote);$upper = strtoupper($inquote);
print ("upper=$upper lower=$lower");print ("upper=$upper lower=$lower");
?>?>
The above would output “upper=NOW IS THE The above would output “upper=NOW IS THE TIME lower=now is the time”.TIME lower=now is the time”.
04/19/2304/19/23 CS346 PHPCS346 PHP 2828
The substr() FunctionThe substr() Function
$part = substr( $name, 0, 5);
Assign theextracted sub-string into thisvariable.
Extract from thisstring variable.
Starting position tostart extraction from.
Number of charactersto extract. (If omitted it willcontinue to extract until the endof the string.)
Substr has the following general format:Substr has the following general format:
04/19/2304/19/23 CS346 PHPCS346 PHP 2929
The substr() FunctionThe substr() Function The substr() function enumerates character positions The substr() function enumerates character positions
starting with 0 (not 1), starting with 0 (not 1), For example, in the string “Homer”, the “H” would be position For example, in the string “Homer”, the “H” would be position
0, the “o” would be position 1, the “m” position 2, and so on.0, the “o” would be position 1, the “m” position 2, and so on.
For example, the following would output “Month=12 For example, the following would output “Month=12 Day=25”.Day=25”.
<?php<?php
$date = "12/25/2002";$date = "12/25/2002";
$month = substr($date, 0, 2);$month = substr($date, 0, 2);
$day = substr($date, 3, 2);$day = substr($date, 3, 2);
print ("Month=$month Day=$day");print ("Month=$month Day=$day");
?>?>
04/19/2304/19/23 CS346 PHPCS346 PHP 3030
The substr() FunctionThe substr() Function As another example: It does not include the As another example: It does not include the
third argument (and thus returns a substring third argument (and thus returns a substring from the starting position to the end of the from the starting position to the end of the search string).search string).
<?php<?php
$date = "12/25/2002";$date = "12/25/2002";
$year = substr($date, 6);$year = substr($date, 6);
print ("Year=$year");print ("Year=$year");
?>?>
The above script segment would output The above script segment would output “Year=2002”.“Year=2002”.
04/19/2304/19/23 CS346 PHPCS346 PHP 3131
TIP: Negative Starting value as a substr() TIP: Negative Starting value as a substr() ArgumentArgument
If second argument of substr() is negative, the If second argument of substr() is negative, the starting position will be counted from the starting position will be counted from the right right end instead of left. end instead of left.
For example, the substr() extracts a substring For example, the substr() extracts a substring starting 4 characters from the end of the string.starting 4 characters from the end of the string.
<!php<!php $filename = “mydoc.html”$filename = “mydoc.html” $suffix = substr( $filename, -4);$suffix = substr( $filename, -4); print (“suffix=$suffix”);print (“suffix=$suffix”);?>?>
04/19/2304/19/23 CS346 PHPCS346 PHP 3232
HTML Forms are not part of PHP language but HTML Forms are not part of PHP language but important way to send data to scripts important way to send data to scripts
Creating HTML Input FormsCreating HTML Input Forms
Text BoxRadio Buttons
Check Box
Select Box
Text Area
Submit/Reset button
04/19/2304/19/23 CS346 PHPCS346 PHP 3333
Starting And Ending HTML Starting And Ending HTML FormsForms
<form action="http://webwizard.aw.com/~phppgm/program.php" method="post">
Program to start when form is submitted.
Place form elements between<form> and </form> tags.
.
.
.</form>
Format tosend data.
Forms end with </form>
You can create HTML forms by using the HTML You can create HTML forms by using the HTML <form><form> and and </form></form> tags. tags.
04/19/2304/19/23 CS346 PHPCS346 PHP 3434
Starting And Ending HTML FormsStarting And Ending HTML Forms
You can create HTML forms by using the You can create HTML forms by using the HTML HTML <form><form> and and </form></form> tags tags
<form method=“post” <form method=“post” action=“http://localhost/program.php”>action=“http://localhost/program.php”><!-- Form element inserted here <!-- Form element inserted here <!-- Form element inserted here <!-- Form element inserted here <!-- Form element inserted here <!-- Form element inserted here
</form></form>
04/19/2304/19/23 CS346 PHPCS346 PHP 3535
Creating Form ButtonsCreating Form Buttons You can create submit and reset buttons by You can create submit and reset buttons by
placing the following within <form> & </form> placing the following within <form> & </form> tags.tags.
The submit button will be labeled “Click To The submit button will be labeled “Click To Submit”. The reset button will be labeled “Erase Submit”. The reset button will be labeled “Erase and Restart”.and Restart”.
<input type=”submit” value=”Click To Submit”> <input type=”reset” value=”Erase and Restart”>
Type ofbutton to create Button Label
04/19/2304/19/23 CS346 PHPCS346 PHP 3636
Another Full Script ExampleAnother Full Script Example1.<html>1.<html>2.<head> <title> A Simple Form </title> </head>2.<head> <title> A Simple Form </title> </head>3.<body>3.<body>4.<form action="http://4.<form action="http://localhost/form.php”localhost/form.php” method="post" >method="post" >5. Click submit to start our initial PHP 5. Click submit to start our initial PHP program.program.
6. <br> <input type="submit" value="Click To 6. <br> <input type="submit" value="Click To Submit">Submit">
7. <input type="reset" value="Erase and 7. <input type="reset" value="Erase and Restart">Restart">
8. </form>8. </form>
9. </body> </html>9. </body> </html>
04/19/2304/19/23 CS346 PHPCS346 PHP 3737
A Full Example ...A Full Example ...
The previous code can be executed at The previous code can be executed at
http://localhost/mod1/form.html orhttp://localhost/mod1/form.html or
http://cs346.cs.uwosh.edu/huen/mod1/form.html http://cs346.cs.uwosh.edu/huen/mod1/form.html
04/19/2304/19/23 CS346 PHPCS346 PHP 3838
name argument for the Submit Button Form name argument for the Submit Button Form ElementElement
Submit button may take a Submit button may take a namename argument, argument, when multiple submit buttons exist on a when multiple submit buttons exist on a formform
Use: to enable the receiving script to Use: to enable the receiving script to identify which button the user has clicked. identify which button the user has clicked.
04/19/2304/19/23 CS346 PHPCS346 PHP 3939
Creating Text Input BoxesCreating Text Input Boxes Text input boxes create a form element for receiving a Text input boxes create a form element for receiving a
single line of text input.single line of text input.
Will be 15 characters wide accepting a maximum of 20 Will be 15 characters wide accepting a maximum of 20 characters. Will set a variable named characters. Will set a variable named fname fname with value with value of whatever the end-user enter.of whatever the end-user enter.
Name: <input type="text" size="15" maxlength="20" name="fname">
Maximum number of inputcharacters
The width of text box. Use this name to identify the formelement in the receiving program.
Create a text box.
04/19/2304/19/23 CS346 PHPCS346 PHP 4040
Creating Password BoxesCreating Password Boxes Password boxes similar to text boxes except asterisks Password boxes similar to text boxes except asterisks
are displayed (instead of text input).are displayed (instead of text input).
Will be 15 characters wide accepting a maximum of 20 Will be 15 characters wide accepting a maximum of 20 characters. Will set a variable named characters. Will set a variable named pass1 pass1 with value with value of whatever the end-user enter.of whatever the end-user enter.
<input type="password" size="15" maxlength="20" name="pass1">
Maximum number ofinput characters
The width of text box.This variable name will be setin the receiving PHP script.
Create a passwordtext box.
04/19/2304/19/23 CS346 PHPCS346 PHP 4141
Warning: Password Boxes Warning: Password Boxes Not SecureNot Secure
When the user submits the form, any data input is When the user submits the form, any data input is sent in clear text (nonencrypted) just like any sent in clear text (nonencrypted) just like any other HTML form field. other HTML form field.
Someone with network access could, therefore, Someone with network access could, therefore, read the password being transferred. read the password being transferred.
For this reason, most Web applications do not use For this reason, most Web applications do not use this approach to receive and transmit passwords.this approach to receive and transmit passwords.
04/19/2304/19/23 CS346 PHPCS346 PHP 4242
Creating Text AreasCreating Text Areas The following creates a text area containing 4 rows and The following creates a text area containing 4 rows and
50 columns. 50 columns.
The words “Your comments here” are the default The words “Your comments here” are the default text.The variable name text.The variable name CommentsComments will be available to will be available to the form-handling script. the form-handling script.
<textarea rows="4" cols="50" name="Comments">Your comments here</textarea>
Number of columns.Number ofrows
Text areas haveclosing tags.
Any text here will appear asdefault text in text area.
04/19/2304/19/23 CS346 PHPCS346 PHP 4343
Creating Radio ButtonsCreating Radio Buttons Radio buttons are small circles that can select by clicking Radio buttons are small circles that can select by clicking
them with a mouse. them with a mouse. Only oneOnly one within a group can be within a group can be selected at once.selected at once.
The The namename argument must be the same for all radio argument must be the same for all radio buttons operating together. The buttons operating together. The valuevalue argument sets argument sets the variable value that will be available to the form-the variable value that will be available to the form-processing script. processing script.
The value that will be sent tothe form-processing program.
Since both radio buttons have the same name,the radio buttons will operator together.
This item will bepre-checked whenthe form is viewed.
Create radio button.
<input type=”radio” name=”contact” value=”Yes” checked> <input type=”radio” name=”contact” value=”No” >
04/19/2304/19/23 CS346 PHPCS346 PHP 4444
Creating Check BoxesCreating Check Boxes Check boxes are small boxes on a form that create a Check boxes are small boxes on a form that create a
check mark when the user clicks them. check mark when the user clicks them.
The above create four independent check boxes; that is, all The above create four independent check boxes; that is, all four check box elements can be selected and each will set a four check box elements can be selected and each will set a value for a different variable name. value for a different variable name.
The value that will be sent to theform-processing program.Each check box sets a different
variable name when selected.
This item will be pre-checkedwhen the form is viewed.
Createcheckbox
<input type=”checkbox” name=”walk” value=”Yes” checked> Walk <input type=”checkbox” name=”Bicycle” value=”Yes”> Bicycle <input type=”checkbox” name=”Car” value=”Yes”> Car <input type=”checkbox” name=”Plane” value=”Yes”> Plane
04/19/2304/19/23 CS346 PHPCS346 PHP 4545
Creating Check BoxesCreating Check Boxes Might want to create a set of check boxes that use the Might want to create a set of check boxes that use the
same same namename argument. argument.
The value received by the form-processing script The value received by the form-processing script would be a comma-separated list of all items checked. would be a comma-separated list of all items checked.
The value that will be sent tothe form-processing program.
Since each checkbox element has the same name,multiple values can be set for the same variable name.
This item will be pre-checkedwhen form is viewed.
Createcheckbox
<input type=”checkbox” name=”travel” value=”Car” checked> Car? <input type=”checkbox” name=”travel” value=”Bike”> Bicycle? <input type=”checkbox” name=”travel” value=”Horse”> Horse? <input type=”checkbox” name=”travel” value=”None”> None of the above?
04/19/2304/19/23 CS346 PHPCS346 PHP 4646
Creating Selection ListsCreating Selection Lists Creates a box with a scrolling list of one or more items that user can Creates a box with a scrolling list of one or more items that user can
highlight and select.highlight and select.
This HTML code creates four options formatted in a scrolling list. This HTML code creates four options formatted in a scrolling list. Only two of these options are displayed at the same time, and the Only two of these options are displayed at the same time, and the user can select more than one option. Multiple selections are sent to user can select more than one option. Multiple selections are sent to
the form-processing script as a comma-separated list.the form-processing script as a comma-separated list.
Allows end-user toselect multiple items.
Viewable window size
<select name="Accommodations" size=2 multiple> <option> A fine hotel </option> <option selected> A cheap motel! </option> <option> A tent in the parking lot </option> <option> Just give me a sleeping bag checked </option> </select>
Variable name set inthe receiving script.
This text is displayed as an option and the entire textwill be returned as the variable's value if selected.
04/19/2304/19/23 CS346 PHPCS346 PHP 4747
Receiving Form Input into PHP Receiving Form Input into PHP Scripts (OLD and INSECURE)Scripts (OLD and INSECURE)
To receive HTML form input into a PHP script: To receive HTML form input into a PHP script:
Use a PHP var name that matches the variable defined in the Use a PHP var name that matches the variable defined in the form element’s form element’s namename argument. argument.
For example, if form uses the following: For example, if form uses the following: <input type=<input type=""radioradio"" name= name=""contactcontact"" value= value=""YesYes"">>
Then form-handling PHP script could use a variable called Then form-handling PHP script could use a variable called $contact. $contact. If the user clicks the radio button, thenIf the user clicks the radio button, then $contact $contact would = would = YesYes
04/19/2304/19/23 CS346 PHPCS346 PHP 4848
Full ExampleFull Example Suppose HTML form uses the following: Suppose HTML form uses the following:
Enter email address: Enter email address: <input type="text" size="16" <input type="text" size="16" maxlength="20" maxlength="20" name="email">name="email">
The php script may useThe php script may use $email to refer to the value of email from the $email to refer to the value of email from the
browser in a user requestbrowser in a user request
04/19/2304/19/23 CS346 PHPCS346 PHP 4949
HTML file:<form action="http://localhost/m06/6-0form_global_on.php" method="post" >
<label>Enter email address:</label> <input type="text" size="16" maxlength="20" name="email" /> <br />May we contact you? Yes<input type="radio" name="contact" value="Yes" checked /> No <input type="radio" name="contact" value="No" /> <br /><br />
<input type="SUBMIT" value="Click To Submit" /> <input type="RESET" value="Erase and Restart" />
</form>
04/19/2304/19/23 CS346 PHPCS346 PHP 5050
Response from PHPResponse from PHP
<html><html><head><title> Receiving Input </title> </head><head><title> Receiving Input </title> </head><body><body><font size=5>Thank You: Got Your Input.</font><font size=5>Thank You: Got Your Input.</font><?php<?php
print ("<br>Your email address is $email");print ("<br>Your email address is $email");
print ("<br> Contact preference is $contact");print ("<br> Contact preference is $contact");
?>?>
04/19/2304/19/23 CS346 PHPCS346 PHP 5151
A Full Example ...A Full Example ...The previous code can be executed atThe previous code can be executed at
http://localhost/m06/6-0form_global_on.htmlhttp://localhost/m06/6-0form_global_on.html
AndAnd
http://localhost/m06/6-0/form_global_on.phphttp://localhost/m06/6-0/form_global_on.php
04/19/2304/19/23 CS346 PHPCS346 PHP 5252
Register_Globals?Register_Globals?
Since PHP 4.2.1, the default PHP configuration Since PHP 4.2.1, the default PHP configuration requires a different mechanism to receive input for requires a different mechanism to receive input for security reasons (than the one just shown)security reasons (than the one just shown) PHP configuration option to turn REGISTER_GLOBALS PHP configuration option to turn REGISTER_GLOBALS
OFF (new default) or OFF (new default) or
ON in the php.ini configuration file.ON in the php.ini configuration file. If your site has REGISTER_GLOBALS OFF you If your site has REGISTER_GLOBALS OFF you
must use a different mechanism to receive HTML must use a different mechanism to receive HTML Form Variables. Form Variables.
04/19/2304/19/23 CS346 PHPCS346 PHP 5353
How can you tell if How can you tell if Register_Globals is OFF?Register_Globals is OFF?
Enter the following PHP script and run it. Enter the following PHP script and run it. <?PHP phpinfo(); ?><?PHP phpinfo(); ?> Search through the output for REGISTER_GLOBALS and Search through the output for REGISTER_GLOBALS and
see if it is set to OFF or ON. see if it is set to OFF or ON.
OR: search for REGISTER_GLOBALS in the file OR: search for REGISTER_GLOBALS in the file php.ini on your serverphp.ini on your server
If it is off, how do you receive input data?If it is off, how do you receive input data? Research for a solutionResearch for a solution