Vous êtes sur la page 1sur 12

Conditionals & Forms

Processing forms

conditional statements
if (condition) { statement1; statement2; etc } else { statements }

if ($orderval > $creditlimit) { $orderOK=false; print(You have exceeded your credit limit); }
if ($orderOK) print(something);
note: no {} needed if one statement

if (cond) { do these if cond is true } elseif (cond2) { do these if cond 1 false and cond2 is true } elseif (cond3) { do these if cond 1 and 2 false and cond3 is true } else { do this if all conditions false }


ternary operator


same as if ($a>$b) {

} else {

switch(expr) { case value1
statements break; if (expr == value1) { statements; } elseif (expr == value2) { statements { else { default statements }

case value2
statements break;

default statements

<form name="form1" method="post" action= "formproc.php" > <input type="text" name="name"> value is input text <input type="hidden" name="hid1" value="secret"> <select name="country"> <option value="CYM">Wales</option> DROPDOWN list <option value="ALB">Scotland</option>
<option value="CAN">Canada </option> <option value="DEU">Germany </option> <option value="FRA">France </option> <option value="GBR">United Kingdom (other) </option> <option value="VAT">Vatican City State (Holy See) </option>


if size is set becomes list if multiple added becomes multi-selection list

</select> <input type="radio" name="food" value="Main food"><br> <input type="radio" name="food" value="Sometimes"><br> <input type="submit" name="Submit" value="Submit"> </form>

sending form data

The name/value pairs are appended to the url in the action part of the form thus:GET http//a.com/formpro.php?nam1=fred&opt=12... name and value are url encoded


query string

POST http//a.com/formpro.php content part nam1=fred values can be much longer than in url values are hidden from user opt=12... name and value may be url encoded

Forms Processing
could be a php file

<form action=xx.php
form calls a script (as in using Perl) which processing script xx.php
checks entered form data actions data send thank you page


<form action=form2.php

form calls itself to do the processing

Forms variables
POST Method $_POST(varName)
returns value of POST variable varName $cName=$_POST(name);

GET Method $_GET(varName)

returns value of GET variable varName $cName=$_GET(name);

note $_GET works for PHP version 4 and up $HTTP_GET_VARS works in all versions If register_globals is set the form variables can be accessed by using $ varName the name in the name/value pair sent with GET or POST. Setting register_globals on is insecure!

Other Variables
the web address of the file (less machine name e.g /webscr/L3/afile.php

The page requesting current page

set to POST or GET

Some PHP functions

gettype($value) returns type of variable isset($var) true if variable assigned htmlspecialchars($string) turns mark-up to text
<b> becomes &lt;b&gt;

mail(to,subject,message) sends message include('style.php') includes a file as include but if file not require('style.php)
loaded throws a fatal error