php forms functions objects css l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
PHP – Forms /Functions/Objects/CSS PowerPoint Presentation
Download Presentation
PHP – Forms /Functions/Objects/CSS

Loading in 2 Seconds...

play fullscreen
1 / 27

PHP – Forms /Functions/Objects/CSS - PowerPoint PPT Presentation


  • 155 Views
  • Uploaded on

PHP – Forms /Functions/Objects/CSS. HTML Forms are used to select different kinds of user input. Make your form using your favourite tool eg netscape composer, frontpage etc Set the form action attribute to <form action="<?php echo $PHP_SELF; ?>" method="post"> or

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PHP – Forms /Functions/Objects/CSS


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
php forms functions objects css
PHP – Forms /Functions/Objects/CSS
  • HTML Forms are used to select different kinds of user input.
  • Make your form using your favourite tool eg netscape composer, frontpage etc
  • 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
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 $_POST, $_GET, $_FILES according to the source of the variable in question. (Older versions of PHP used $HTTP_POST_VARS, $HTTP_GET_VARS, $HTTP_POST_FILES respectively)

  • 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 $_POST['username'] ; this is an associative array.

Example – a decimal to roman numeral converter.

http://sng.its.monash.edu.au:7747/rnc.html

form eg1 cont
Form eg1 cont.

<HTML>

<HEAD>

<TITLE>Roman Numeral Converter</TITLE>

</HEAD>

<BODY>

<FORM METHOD="POST" ACTION="rnc.php">

Enter a numeric value to be converted:

<BR><INPUT TYPE="TEXT" NAME="number">

<input type="submit" value="Submit">

</FORM>

</BODY>

</HTML>

form elements
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 Input
  • http://www.w3schools.com/html/html_forms.asp?output=print
  • Look at form examples on the site.
  • Eg mail example:
  • http://www.w3schools.com/html/tryit.asp?filename=tryhtml_form_mail
form example from prac 2
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
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 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
  • $_POST- an associative array of variables passed to the current script via the HTTP POST method.
  • $_GET- an associative array of variables passed to the current script via the HTTP GET method.
  • $_ENV- an associative array of variables passed to the current script via the parent environment.
  • $_SERVER- the name of the server host under which the current script is executing. The following are some of the variables in this array
    • 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 http://sng.its.monash.edu.au:7747/phpinfo.php
form example 2
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($_POST))

{

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

}

email form example continued
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);

}

?>

  • See FAQ re mail in PHP.
using custom functions
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);

}

?>

slide11
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
functions
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
default arguments
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);

function by value or by reference
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”; }

class
Class
  • A class is a collection of variables and functions working with these variables. A class is defined using the following syntax:
  • <?phpclass Cart {   var $items;  // Items in our shopping cart  // Add $num articles of $artno to the cart  function add_item($artno, $num) {       $this->items[$artno] += $num;   }   // Take $num articles of $artno out of the cart    function remove_item($artno, $num) {       if ($this->items[$artno] > $num) {           $this->items[$artno] -= $num;           return true;       } else {           return false;       }  }}?>
class 2
Class -2
  • Classes are types, that is, they are blueprints for actual variables. You have to create a variable of the desired type with the new operator.
  • <?php$cart = new Cart;$cart->add_item("10", 1);$another_cart = new Cart;$another_cart->add_item("0815", 3);?>
  • This creates the objects $cart and $another_cart, both of the class Cart. The function add_item() of the $cart object is being called to add 1 item of article number 10 to the $cart. 3 items of article number 0815 are being added to $another_cart.
class 3
Class-3
  • Note that the var is named $cart->items, not $cart->$items, that is, a variable name in PHP has only a single dollar sign.
  • Within a class definition, you do not know under which name the object will be accessible in your program: at the time the Cart class was written, it was unknown that the object will be named $cart or $another_cart later. Thus, you cannot write $cart->items within the Cart class itself. Instead, in order to be able to access it's own functions and variables from within a class, one can use the pseudo-variable $this which can be read as 'my own' or 'current object'. Thus, '$this->items[$artno] += $num' can be read as 'add $num to the $artno counter of my own items array' within the current object'.
class 4
Class - 4

A database ‘class’ could be used with functions like…

function db_connect($conn_type = 'P')

{

global $ORACLE_USERNAME, $ORACLE_PASSWORD;

//use a persistant connection as oracle connections seem REALLY slow

if( $conn_type == 'P' )

$conn=ociplogon($ORACLE_USERNAME,$ORACLE_PASSWORD,DATABASE);

else if ($con_type == 'N' )

$conn = cinlogon($ORACLE_USERNAME,$ORACLE_PASSWORD,DATABASE);

else

$conn = ocilogon($ORACLE_USERNAME,$ORACLE_PASSWORD,DATABASE);

if( !$conn )

{ echo "<h1>Could not connect to database!. ABORTING!</h1>\n";

exit;}

return $conn;

}

cascading style sheets css
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
  • http://www.its.monash.edu.au/web/slideshows/intro_css2/
style sheets can save a lot of work
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
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
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
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

}

slide24

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>

slide25

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%;…..}

css in action
CSS in Action.
  • http://www.w3schools.com/css/showit.asp?filename=ex1
  • http://www.w3schools.com/css/showit.asp?filename=ex2
  • For more information on CSS see
  • http://www.w3schools.com/css/css_howto.asp
next lecture
Next Lecture…
  • How to ‘remember’ what the user has done from one page to the next ?
    • Cookies : - client side
    • Sessions : - server side