php for forms html forms are used to select different kinds of user input. make your form using your...

21
PHP for Forms HTML Forms are used to select different kinds of user input. Make your form using your favourite tool Set the form action attribute to <form action="<?php echo $PHP_SELF; ?>" method="post"> or <form action="script.php" method="post">; Make sure that you name each form field that you want to process as these names will be available to the processing script as variables <input type="text" name="inputtext"> $inputtext will contain whatever is typed into the text field

Post on 19-Dec-2015

216 views

Category:

Documents


1 download

TRANSCRIPT

PHP for Forms• HTML Forms are used to select different kinds of

user input.• Make your form using your favourite tool • Set the form action attribute to

– <form action="<?php echo $PHP_SELF; ?>" method="post">

– or – <form action="script.php" method="post">;

• Make sure that you name each form field that you want to process as these names will be available to the processing script as variables– <input type="text" name="inputtext"> – $inputtext will contain whatever is typed into the text

field

Form example 1

• When a form is submitted to a PHP script, any variables from that form will be automatically made available to the script by PHP. If the track_vars configuration option is turned on, then these variables will be located in the associative arrays $HTTP_POST_VARS, $HTTP_GET_VARS, and/or $HTTP_POST_FILES, according to the source of the variable in question.

• Example. Simple form variable <form action="foo.php" method="post"> Name: <input type="text" name="username"><br> <input type="submit"> </form> When the above form is submitted, the value from the text input will be

available in $HTTP_POST_VARS['username'] ; this is an associative array.

Form Elements•text fields, textarea fields, hidden fields, passwords, buttonsdrop-down menus, radio buttons, checkboxes, etc•POST or GET form methods (how form data is passed)

•GET appends data to URL and is then available as environment variables•POST includes data in the HTTP request body

•Action= tag specifies server program/script to execute See:HTML Forms and Inputhttp://www.w3schools.com/html/html_forms.asp?output=print

Form Example (from prac 2)<HTML><HEAD><TITLE>Project 6-1</TITLE></HEAD><BODY><!-- File p-6-1.html --><FORM METHOD="POST" ACTION="p-6-1.php">Enter a numeric value:<BR><INPUT TYPE="TEXT" NAME="number"></FORM></BODY></HTML>

Form Example Processing (from prac 2)

<HTML><HEAD><TITLE>Project 6-1</TITLE></HEAD><BODY><!-- File p-6-1.php --><?php echo "The number entered was: $number."; if ($number > 10) echo "<BR>That's a big number.";?></BODY></HTML>

PHP Predefined variables• PHP has a range of predefined variables available - for example Apache

variables, environment variables and PHP-specific variables• $PHP_SELF - the filename of the currently executing script • $HTTP_POST_VARS - an associative array of variables passed to the

current script via the HTTP POST method. • $HTTP_GET_VARS - an associative array of variables passed to the current

script via the HTTP GET method. • $HTTP_ENV_VARS - an associative array of variables passed to the current

script via the parent environment. • $SERVER_NAME - the name of the server host under which the current

script is executing. • $DOCUMENT_ROOT - the document root directory under which the current

script is executing, as defined in the server's configuration file. • $HTTP_REFERER - the address of the page (if any) which referred the

browser to the current page. • REMOTE_ADDR - IP address of the client• REMOTE_HOST - Host name of the client eg browser• Etc – demo phpinfo()

Form example 2

This is an example of a script which displays a form and processes the input by constructing and sending an email.

<?php

// test if form has been filled in

if (!isset($submit))

{

// output form here – HTML form tags etc

}

else

{

while(list($key, $val) = each($HTTP_POST_VARS))

{

echo "Variable $key contains <strong>$val</strong><br />";

}

Email form example continued.// create an email message // subject line $subject = "Input from my comment form"; // generate email headers $headers = "From: ".$email."\r\n"; $headers .= "Cc: ".$email."\r\n"; $headers .= "Content-type: text/plain\r\n"; $headers .= "X-mailer: PHP/ ".phpversion(); // set the to address $to = "[email protected]"; // build the message $msg = "From $fname $lname\n\n".$comment; // use PHP's mail function, which uses sendmail to send mail mail($to, $subject, $msg, $headers); }?>

Using Custom FunctionsIf you have some things that you do in a number of different scripts, you might considerputting them into custom functions. You could collect them into a file called functions.php and include them in all your scripts, or you could name them individually and include them only as needed. For example, you might want to make your own mail function which includes somedefault values. To make it flexible, you will want to pass it information (arguments) to use in different circumstances. <?php function my_mail ($subject, $msg, $address="[email protected]") { $headers = "From: ".$address."\r\n"; if (!(strcmp($address, "[email protected]"))) { $headers .= "Cc: ".$address."\r\n"; }

$headers .= "Content-type: text/plain\r\n"; $headers .= "X-mailer: PHP/ ".phpversion(); $to = "[email protected]"; mail($to, $subject, $msg, $headers); } ?>

• To use this function, you would type

my_mail("My sample subject", $msg, $email);

Inside the function,

$subject will be set to My sample subject,

$msg will be set to the value of the variable $msg in the script

$address will be set to the value of the variable $email in the script

• This assumes that the statement:require (“functions.php”);

Had been included before the call.

• Require files can be used for html page headers and footers

FunctionsEg.function compute_area($height, $width){

return $height*$width}• Function names are not case sensitive• Return statement terminates function

– Exit() terminates script– If no return statement NULL is returned

Default ArgumentsEg. function gst($amount, $rate=0.12){

Return $amount*$rate;}– May be called using – to override $rate:

$tax = gst($purchase, 0.08);

Or to use default rate $tax = gst($purchase);

Function – by value or by reference

function byvalue($x){ echo “<BR>x=$x”; } //this is the php default

function byreference(&$y){ echo “<BR>y=$y”; }

• Note that byvalue(&$num) // also achieves a by reference call.

Cascading style sheets - CSS.

• CSS stands for Cascading Style Sheets • Styles define how to display HTML elements • Styles are normally stored in Style Sheets • Styles were added to HTML 4.0 to solve the adding of

new tags to html• External Style Sheets can save you a lot of work • External Style Sheets are stored in CSS files • Multiple style definitions will cascade into one

according to priority rules.• http://www.w3schools.com/css/demo_default.htm

Style Sheets Can Save a Lot of Work• Styles in HTML 4.0 define how HTML elements

are displayed, just like the font tag and the color attribute

• External style sheets enable you to change the appearance and layout of all the pages in your Web, just by editing a single CSS document.– Eg. change the font or color of all the headings in all

your Web pages

• CSS is a breakthrough in Web design because it allows developers to control the style and layout of multiple Web pages all at once.

• Can define a style for each HTML element.

Cascading Order • What style will be used when there is more than

one style specified for an HTML element?– styles will "cascade" into a new "virtual" Style Sheet by

the following rules, where number four has the highest priority:

– 1.Browser default – 2.External Style Sheet – 3.Internal Style Sheet (inside the <head> tag) – 4.Inline Style (inside HTML element) So, an inline style (inside an HTML element) has the

highest priority, which means that it will override every

style declared inside the <head> tag, in an external style sheet, and in a browser (a default value).

CSS Syntax• The CSS syntax is made up of three parts: a

selector, a property and a value:– selector {property: value}– The selector is normally the element/tag you

wish to define, the property is the attribute you wish to change, and each property can take a value.

– The property and value are separated by a colon and surrounded by curly braces eg.

body {color: black}

CSS examples• Readability:- describe one property on each line,

p {text-align: center; color: black; font-family: arial}

• Grouping multiple elements

h1, h2, h3, h4, h5, h6

{

color: green

}

The class Attribute -define different styles for the same element. Eg. p.right {text-align: right} p.center {text-align: center}You have to use the class attribute in your HTML document: <p class="right"> This paragraph will be right-aligned. </p> <p class="center"> This paragraph will be center-aligned. </p> You can also omit the tag name in the selector to define a style that can be used by many elements: .center {text-align: center}<h1 class="center"> This heading will be center-aligned</h1> <p class="center"> This paragraph will also be center-aligned.</p>

The id Attribute can be defined in two ways. It can be defined to match all elements with a particular id, or to match only one element with a particular id. <p id="intro"> This paragraph will be right-aligned. </p>

In this example the id attribute will match all elements with id="intro": #intro

{ font-size:110%; font-weight:bold; color:#0000ff; background-color:transparent }In this example the id attribute will match only p elements with id="intro" p#intro { font-size:110%;…..}