Aims:
<form action="guestbook.php" method="get"> <label for="firstnameField">First name:</label> <input type="text" name="first" id="firstnameField" /> <label for="surnameField">Surname:</label> <input type="text" name="surname" id="surnameField" /> <input type="submit" value="Submit" /> <input type="reset" /> </form>
guestbook.php script
$_GET
name
attributes in the (X)HTML$_GET['first']
accesses whatever the user typed as first nameguestbook.php script
<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Welcome!</title> </head> <body> <p> <?php echo "You are {$_GET['surname']}, {$_GET['first']} {$_GET['surname']}."; ?> </p> </body> </html>
form
, use method="post"
$_POST
if (condition)
{
do this if condition is true
}
age.html
: an (X)HTML form that requests the user's agetestage.php
: a PHP script that outputs different
messages, based on the user's age
<?php if ($_GET['age'] < 18) { echo 'CS1102 is a module for adults. Come back when you\'re older.'; } if ($_GET['age'] >= 18) { echo 'Welcome to CS1102!'; } ?>
else
to specify a sequence of statements to be
executed when the condition is false:
if (condition)
{
do this if condition is true
}
else
{
do this if condition is false
}
<?php if ($_GET['age'] < 18) { echo 'CS1102 is a module for adults. Come back when you\'re older.'; } else { echo 'Welcome to CS1102!'; } ?>
$x
and $y
, the operators
are:
$x == $y | $x != $y |
$x < $y | $x >= $y |
$x > $y | $x <= $y |
$x = $y
instead of $x == $y$
to test for equality?$_GET['age']
to 18.
What?
strcmp($s1, $s2)
$x
and $y
are equal
and of the same type, use
$x === $y
$x
and $y
are not equal
or not of the same type, use
$x !== $y
and
)
if ($age > 16 && $age <= 66) ...
or
)
if ($age == 17 || $age == 18) ...
if (! $age >= 21) ...
<form action="personality.php" method="get"> <p> Which one of these best describes your personality? </p> <p> <input type="radio" name="pers" value="lust" id="lustBtn" checked="checked" /> <label for="lustBtn">Lustful</label> <input type="radio" name="pers" value="sloth" id="slothBtn" /> <label for="slothBtn">Slothful</label> <input type="radio" name="pers" value="greed" id="greedBtn" /> <label for="greedBtn">Gluttonous</label> </p> <p> Check the box if you ever have murderous thoughts? </p> <p> <input type="checkbox" name="murder" value="yes" /> </p> <p> <input type="submit" value="Submit" />; </p> </form>
<input type="checkbox" name="murder" value="yes" />
type="checkbox"
name
and value
are sent to the serverchecked="checked"
attribute if you want it initially to be checked
<input type="radio" name="pers" value="lust" />
<input type="radio" name="pers" value="sloth" />
<input type="radio" name="pers" value="greed" />
type="radio"
name
and value
are sent to the serverchecked="checked"
attribute so that one is
initially checkedpersonality.php
Following extensive analysis of your personality, we offer you this advice: <?php $pers = $_GET['pers']; $murder = $_GET['murder']; if ($pers == 'greed' && $murder == 'yes') { echo 'Be careful. One day, you might eat someone.'; } elseif ($pers == 'sloth') { if ($murder == 'yes') { echo 'Don\'t worry. You\'ll never murder anybody: '; } echo 'You\'re a lazy b*****d!'; } else { echo 'You\'re not an interesting person. Go away.'; } ?>
personality.php
if...elseif...else...
if
inside another