Presentation is loading. Please wait.

Presentation is loading. Please wait.

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 –

Similar presentations


Presentation on theme: "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 –"— Presentation transcript:

1 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 – " method="post"> – or – ; 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 – – $inputtext will contain whatever is typed into the text field

2 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 Name: 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.

3 Form Elements text fields, textarea fields, hidden fields, passwords, buttons drop-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 InputHTML Forms and Input http://www.w3schools.com/html/html_forms.asp?output=print

4 Form Example (from prac 2) Project 6-1 Enter a numeric value:

5 Form Example Processing (from prac 2) Project 6-1 <?php echo "The number entered was: $number."; if ($number > 10) echo " That's a big number."; ?>

6 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()

7 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 $val "; }

8 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 = "lisa.wise@its.monash.edu.au"; // 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); } ?>

9 Using Custom Functions If you have some things that you do in a number of different scripts, you might consider putting 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 some default 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="lisa.wise@its.monash.edu.au") { $headers = "From: ".$address."\r\n"; if (!(strcmp($address, "lisa.wise@its.monash.edu.au"))) {$headers.= "Cc: ".$address."\r\n"; } $headers.= "Content-type: text/plain\r\n"; $headers.= "X-mailer: PHP/ ".phpversion(); $to = "lisa.wise@its.monash.edu.au"; mail($to, $subject, $msg, $headers); } ?>

10 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

11 Functions Eg. 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

12 Default Arguments Eg. 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);

13 Function – by value or by reference function byvalue($x) { echo “ x=$x”; } //this is the php default function byreference(&$y) { echo “ y=$y”; } Note that byvalue(&$num) // also achieves a by reference call.

14 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

15 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.

16 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 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 tag, in an external style sheet, and in a browser (a default value).

17 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}

18 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 }

19 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: This paragraph will be right-aligned. This paragraph will be center-aligned. 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} This heading will be center-aligned This paragraph will also be center-aligned.

20 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. This paragraph will be right-aligned. 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%;…..}

21 CSS in Action. http://www.w3schools.com/css/showit.asp?filena me=ex1http://www.w3schools.com/css/showit.asp?filena me=ex1 http://www.w3schools.com/css/showit.asp?filena me=ex2http://www.w3schools.com/css/showit.asp?filena me=ex2 For more information on CSS see http://www.w3schools.com/css/css_howto.asp


Download ppt "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 –"

Similar presentations


Ads by Google